Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically switch colors in Android frame layout

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report