处理程序不更新UI

时间:2017-01-20 17:19:56

标签: java android android-handler

首先,我是android新手。因此,我使用处理程序在MainActivity.java中的 TextView 上显示当前时间,该工作正常。然后我使用TimePicker(实际上是另一个活动,TimeActivity.java)来选择时间停止的时间。

例如,如果使用TimePicker选择的时间是8:30,则TextView中的时间将停止进一步超过8:30

还有一个STOP按钮,用于调用 handler.removeMessages(0); 来停止处理程序,从而停止时间。

  • 问题是,当我从TimeActivity.java回来时, TextView 是空白的,看起来处理程序没有运行但是它成功Toast“Time Stopped”当时间到了。这是什么异常?
  • 我碰巧遇到了另一个问题,Toast, stopTime()中的“停止时间”在屏幕上占优势的时间过长,但在停止时Toast是正常的(查看v)即使我使用相同的逻辑来停止两种方法中的处理程序。是什么导致了这个?

这是我的MainActivity.java

import static com.gamecodeschool.broadcast.TimeActivity.minute1;

公共类MainActivity扩展了AppCompatActivity {

Button start,stop,set;
TextView textView;
final Handler handler = new Handler();
Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Views
    textView = (TextView)findViewById(R.id.textView);
    start = (Button)findViewById(R.id.button);
    stop = (Button)findViewById(R.id.button2);
    set = (Button)findViewById(R.id.button3);

    set.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(getApplicationContext(),TimeActivity.class);
            startActivity(intent);
        }
    });
}

public void stop(View v) {
    handler.removeMessages(0);
    Toast.makeText(this,"Stopped",Toast.LENGTH_SHORT).show();
}

public void runInBackground(View v) {
    handler.post(r);
    r = new Runnable() {
        @Override
        public void run() {
            //String time = java.text.DateFormat.getDateTimeInstance().format(new Date());
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            StringBuilder stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
            textView.setText(stringBuilder);
            if (minute==minute1){
                stopTime();
            }
            handler.postDelayed(this,100);
        }
    };
}

private void stopTime() {
    handler.removeMessages(0);
    Toast.makeText(this,"Time Stopped",Toast.LENGTH_SHORT).show();
} }

这是我的TimeActivity

public class TimeActivity extends AppCompatActivity {

TimePicker timePicker;
Button button;
TextView textView;
StringBuilder stringBuilder;
static int hour1,minute1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time);

    timePicker = (TimePicker)findViewById(R.id.timePicker);
    button = (Button)findViewById(R.id.saveButton);
    textView = (TextView)findViewById(R.id.timeView);
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    showTime(hour,minute);
}

private void showTime(int hour, int minute) {
    stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
    textView.setText(stringBuilder);
}

public void saveTime(View v){
    hour1 = timePicker.getHour();
    minute1 = timePicker.getMinute();
    showTime(hour1,minute1);
    Toast.makeText(TimeActivity.this,hour1+":"+minute1,Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this,MainActivity.class);
    startActivity(intent);
} }

1 个答案:

答案 0 :(得分:0)

更新用户界面必须在UIThread上完成,例如更新您的TextView,例如:

public void runInBackground(View v) {
    handler.post(r);
    r = new Runnable() {
        @Override
        public void run() {
            //String time = java.text.DateFormat.getDateTimeInstance().format(new Date());
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            StringBuilder stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
            runOnUiThread(new Runnable() {
                @Override public void run() {
                    textView.setText(stringBuilder);
                }
            });
            textView.setText(stringBuilder);
            if (minute==minute1){
                stopTime();
            }
            handler.postDelayed(this,100);
        }
    };
}