Get the App
SLTechnology News&Howtos  ›  Development  › 

How to automatically switch colors in Android frame layout

Shulou Source: shulou.com Published: 2022-05-31 20:07:43 09月26日 Update

This article introduces the "Android frame layout how to automatically switch colors" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Effect:

Achieve:

Activity_main.xml

ActivityMain.java

Import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {private TextView tvBottom; private TextView tvMiddle; private TextView tvTop; private int [] colors; private Handler handler; private Thread thread; private boolean isRunning @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / set the user interface setContentView (R.layout.activity_main) using the layout resource; / / get the control instance tvBottom = findViewById (R.id.tvBottom) through the resource identifier; tvMiddle = findViewById (R.id.tvMiddle); tvTop = findViewById (R.id.tvTop) / / initialize the color array colors = new int [] {Color.RED, Color.BLUE, Color.GREEN}; handler = new Handler () {@ Override public void handleMessage (@ NonNull Message msg) {super.handleMessage (msg) If (msg.what = = 0x0001) {/ / switch color int temp = colors [0]; for (int I = 0; I < colors.length-1; iSue +) {colors [I] = colors [I + 1] } colors [colors.length-1] = temp; / / set the background color of the three-layer label according to the switched color array tvBottom.setBackgroundColor (colors [0]); tvMiddle.setBackgroundColor (colors [1]); tvTop.setBackgroundColor (colors [2]) };} / * [start] button Click event handling method * / public void doStart (View view) {/ / set thread running control variable isRunning = true / / create child threads to send messages thread = new Thread regularly (new Runnable () {@ Override public void run () {while (isRunning) {/ / send messages handler.sendEmptyMessage (0x0001) to the main thread) / / Let the thread sleep for 500ms try {Thread.sleep (500);} catch (InterruptedException e) {e.printStackTrace ();}) / / start thread thread.start ();} / * [stop] button Click event handling method * / public void doStop (View view) {/ / set thread running control variable isRunning = false; / / destroy child threads thread = null;}}

String.xml

Frame layout: color switching bottom layer, middle layer, top layer begins and ends, "how to automatically switch colors in Android frame layout" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Threads colors switches layouts events contents variables buttons arrays methods more messages knowledge resources processing control running utility learning and then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Shulou Technology OPPO Reno Docker