Android handler常见api

package com.example.a32_common_handler_api;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Timer timer;
    private TimerTask task;
    private static int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);
        //2000毫秒后 执行run方法  delay 延期
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                tv.setText("哈哈");
//            }
//        }, 5000);
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String ii = i+"";
                        tv.setText(ii); //这里之前报了一个错 注意!
                        i++;
                    }
                });
            }
        };
        //3秒后 每隔1秒执行一次run方法  period 周期
        timer.schedule(task, 3000, 1000);
    }
    //当Activity销毁的时候 会执行这个方法

    @Override
    protected void onDestroy() {
        timer.cancel();
        task.cancel();
        super.onDestroy();
    }
}


setText(i);报了一个错。 找不到资源ID?

Do not concatenate text displayed with setText. Use resource string with placeholders. less... (Ctrl+F1)
When calling TextView#setText
* Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.
* Do not pass a string literal (e.g. "Hello") to display text. Hardcoded text can not be properly translated to other languages. Consider using Android resource strings instead.
* Do not build messages by concatenating text chunks. Such messages can not be properly translated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值