编辑activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvBottom"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#ff0000"
android:text="@string/bottom"
android:textColor="#ffff00"
android:textSize="30sp" />
<TextView
android:id="@+id/tvMiddle"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="#0000ff"
android:text="@string/middle"
android:textColor="#ffff00"
android:textSize="30sp" />
<TextView
android:id="@+id/tvTop"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#00ff00"
android:text="@string/top"
android:textColor="#ffff00"
android:textSize="30sp" />
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:onClick="doStart"
android:text="开始"
android:textSize="20dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:onClick="doStop"
android:text="停止"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
图像界面:
编辑MainActivity.java
代码如下:
package net.zxj.HomeWork09;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvBottom;
private TextView tvMiddle;
private TextView tvTop;
private int[] colors;
private Thread thread;
private boolean isRunning ;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识获得控件实例
tvBottom = findViewById(R.id.tvBottom);
tvMiddle = findViewById(R.id.tvMiddle);
tvTop = findViewById(R.id.tvTop);
// 初始化颜色数组
colors = new int[] {Color.RED, Color.BLUE, Color.GREEN};
}
/**
* 切换颜色单击事件处理方法
*
* @param view
*/
/**
* 按钮单击事件处理方法
*/
public void doStart(View view){
isRunning = true;
thread = new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
int temp = colors[colors.length - 1];
for(int i = colors.length - 1 ; i > 0 ;i--){
colors[i] = colors[i - 1];
}
colors[0] = temp;
//发送消息
handler.sendEmptyMessage(0x00);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]);
}
}
});
Toast.makeText(MainActivity.this,"开始~",Toast.LENGTH_LONG).show();
}
public void doStop(View view) {
Toast.makeText(this,"停止~",Toast.LENGTH_LONG).show();
}
}