Android ——Handler相关

时间:2023-03-10 03:09:59
Android ——Handler相关

layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp2.PractiseActivity4"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvw1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvw2"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btt1"
android:text="随机选择"
android:onClick="btn_onClick"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受的消息"
android:id="@+id/tv_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发消息"
android:onClick="btn2_onClick"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:id="@+id/tv_6"
android:layout_gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加"
android:onClick="btn3_onClick"
android:id="@+id/bt_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="减少"
android:onClick="btn3_onClick"
android:id="@+id/bt_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="btn3_onClick"
android:id="@+id/bt_5"/>
</LinearLayout>

java类:

 package com.hanqi.testapp2;

 import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class PractiseActivity4 extends AppCompatActivity { TextView tvw1;
Button btt1;
TextView tvw2;
TextView tv_5;
TextView tv_6;
Button bt_3;
Button bt_4;
Button bt_5;
//定义Handler
Handler h = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//处理消息
if (msg.what ==1)
{
String m = msg.obj.toString();
tv_5.setText(tv_5.getText()+" "+m);
}
else if (msg.what ==2)
{
tv_5.setText(tv_5.getText()+"空消息");
}
}
}; int i = 10;
Handler hl = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 1:
if (i ==20)
{
return;
}
i++;
tv_6.setText(i + "");
//发送
hl.sendEmptyMessageDelayed(1, 1000);
hl.removeMessages(2);
bt_3.setEnabled(false);
bt_4.setEnabled(true);
bt_5.setEnabled(true);
break;
case 2:
if (i ==0)
{
return;
}
i--;
tv_6.setText(i+"");
hl.sendEmptyMessageDelayed(2, 1000);
hl.removeMessages(1);
bt_3.setEnabled(true);
bt_4.setEnabled(false);
bt_5.setEnabled(true);
break;
case 3:
hl.removeMessages(1);
hl.removeMessages(2);
bt_3.setEnabled(true);
bt_4.setEnabled(true);
bt_5.setEnabled(false);
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise4);
tvw1 = (TextView)findViewById(R.id.tvw1);
btt1 = (Button)findViewById(R.id.btt1);
tvw2 = (TextView)findViewById(R.id.tvw2);
tv_5 = (TextView)findViewById(R.id.tv_5);
tv_6 = (TextView)findViewById(R.id.tv_6);
bt_3 = (Button)findViewById(R.id.bt_3);
bt_4 = (Button)findViewById(R.id.bt_4);
bt_5 = (Button)findViewById(R.id.bt_5);
}
//三个按钮
public void btn3_onClick(View v)
{
switch (v.getId())
{
case R.id.bt_3:
//发送增加消息
hl.sendEmptyMessage(1);
break;
case R.id.bt_4:
hl.sendEmptyMessage(2);
break;
case R.id.bt_5:
hl.sendEmptyMessage(3);
break;
}
}
//发消息
public void btn2_onClick(View v)
{
//启动线程
new Thread(){
@Override
public void run() {
//发送消息
//1.创建Message
Message m = Message.obtain();
m.what = 1;//id
m.obj = "新信息";
//2.发送即时消息
h.sendMessage(m);
m = Message.obtain();
m.what = 1;//id
m.obj = "新信息";
//发送延时消息
h.sendMessageDelayed(m,2000);
h.sendEmptyMessage(2);
h.sendEmptyMessageDelayed(2,1000);
}
}.start();
} // String c1 = "北京";
// String c2 = "上海";
// public void btn_onClick(View v)
// {
// //创建子线程1
// new Thread(){
// @Override
// public void run() {
// for (int i=0;i<20;i++)
// {
// if(i%2==0)
// {
// c1="";
// }
// else
// {
// c1="北京";
// }
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// tvw1.setText(c1);
// }
// });
// //暂停
// try {
// Thread.sleep((int) (Math.random()*1000));
// }
// catch (Exception e)
// {
//
// }
// }
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(PractiseActivity4.this, c1 + "循环完成", Toast.LENGTH_SHORT).show();
// }
// });
// }
// }.start();
// //创建子线程2
// new Thread(){
// @Override
// public void run() {
// for (int i=0;i<20;i++)
// {
// if(i%2==0)
// {
// c2="";
// }
// else
// {
// c2="上海";
// }
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// tvw2.setText(c2);
// }
// });
// //暂停
// try {
// Thread.sleep((int) (Math.random()*1000));
// }
// catch (Exception e)
// {
//
// }
// }
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(PractiseActivity4.this, c2 + "循环完成", Toast.LENGTH_SHORT).show();
// }
// });
// }
// }.start();
// }
}

效果为:

Android ——Handler相关Android ——Handler相关Android ——Handler相关Android ——Handler相关