• 数据更新的基本写法


    private TextView tv;
    private Button btn,btn2;
    private int btnsend;
    private final int HANDLER_TEST=1;
    private final int HANDLER_STOP=0;
    private int count=0;

    private Handler handler=new Handler(){

    @Override
    public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    super.handleMessage(msg);

    // Toast.makeText(MainActivity.this, count, 500).show();
    switch (msg.what) {
    case HANDLER_TEST :
    tv.setText("当前计数值:"+count);
    break;

    default:
    break;
    }

    }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.Tv);
    btn=(Button)findViewById(R.id.button);
    btn2=(Button)findViewById(R.id.button2);
    btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    btnsend=HANDLER_TEST;
    }

    });

    btn2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    btnsend=HANDLER_STOP;
    }

    });


    new Thread(new Runnable() {

    @Override
    public void run() {
    while(true){
    try {

    Thread.sleep(10);
    count++;
    System.out.println(count);
    // btnsend=HANDLER_TEST;
    } catch (Exception e) {
    e.printStackTrace();
    }
    Message msg=new Message();
    msg.what=btnsend;
    handler.sendMessage(msg);

    }
    }
    }).start();
    }

  • 相关阅读:
    VS2010下配置CxImage
    Visual Studio 2010 开发配置
    主机屋使用感受
    Web开发者必备的20款超赞jQuery插件
    自动页面居中
    jQuery+CSS打造的网页背景颜色切换效果
    小按钮,大学问
    【网站开发必备】——12款响应式 Lightbox(灯箱)效果插件
    修正 IE 的双倍页边距 bug
    a>b?a:b
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13050589.html
Copyright © 2020-2023  润新知