标签:
package com.example.lenovo.textapp4;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class Text6Activity extends AppCompatActivity {
TextView tv_5,tv_6;
//定义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:
i++;
if (i==20)
{
return;
}
tv_6.setText(i+"");
//发送
hl.sendEmptyMessageDelayed(1,1000);
hl.removeMessages(2);
break;
case 2:
i--;
if (i==1)
{
return;
}
tv_6.setText(i+"");
hl.sendEmptyMessageDelayed(2,1000);
hl.removeMessages(1);
break;
case 3:
hl.removeMessages(1);
hl.removeMessages(2);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text6);
tv_5=(TextView)findViewById(R.id.tv_5);
tv_6=(TextView)findViewById(R.id.tv_6);
}
//三个按钮
public void bt2_on(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;
}
}
}
标签:
原文地址:http://www.cnblogs.com/1ming/p/5500296.html