定时器的EDITTEXT盒:只有创建视图层次可以触摸其观点原来的线程 [英] Timer for an editText Box : Only the original thread that created a view hierarchy can touch its views
本文介绍了定时器的EDITTEXT盒:只有创建视图层次可以触摸其观点原来的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让EditText上boxvibrate改变颜色瞬间如果输入了错误的密码
最终绘制对象oldBackground = findViewById(R.id.email).getBackground();
TimerTask的TimerTask的=新的TimerTask(){
@覆盖
公共无效的run(){
MainActivty.this.findViewById(R.id.password).setBackground(oldBackground);
MainActivty.this.findViewById(R.id.email).setBackground(oldBackground);
}
};
Toast.makeText(MainActivty.this,valArray.get(0)的ToString(),Toast.LENGTH_SHORT).show();
findViewById(R.id.password).setBackgroundColor(Color.RED);
findViewById(R.id.email).setBackgroundColor(Color.RED);
振动器V =(振动器)getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);定时器定时器=新的Timer();
timer.schedule(TimerTask的,1000);
解决方案
定时任务在不同的线程中运行。用户界面应该是在UI线程上更新。
使用 runOnUiThread
或使用处理程序
。
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){
//在这里更新用户界面
}
});
处理程序
处理程序m_handler;
可运行m_handlerTask;
m_handler =新的处理程序();
m_handlerTask =新的Runnable()
{
@覆盖
公共无效的run(){ // 做一点事
m_handler.postDelayed(m_handlerTask,1000); }
};
m_handlerTask.run();
您也可以使用一个倒数计时器
<一个href=\"http://stackoverflow.com/questions/17620641/countdowntimer-in-minutes-and-seconds/17620827#17620827\">Countdowntimer以分和秒
I am trying to make the edittext boxvibrate and change color momentarily if a wrong password is entered
final Drawable oldBackground = findViewById(R.id.email).getBackground();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
MainActivty.this.findViewById(R.id.password).setBackground(oldBackground);
MainActivty.this.findViewById(R.id.email).setBackground(oldBackground);
}
};
Toast.makeText(MainActivty.this , valArray.get(0).toString(), Toast.LENGTH_SHORT).show();
findViewById(R.id.password).setBackgroundColor(Color.RED);
findViewById(R.id.email).setBackgroundColor(Color.RED);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);Timer timer = new Timer();
timer.schedule(timerTask, 1000);
解决方案
Timer task runs on a different thread. Ui should be update on the ui thread.
Use runOnUiThread
or use a Handler
.
runOnUiThread(new Runnable(){
@Override
public void run(){
// update ui here
}
});
Handler
Handler m_handler;
Runnable m_handlerTask ;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
// do something
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
You can also use a CountDown timer
Countdowntimer in minutes and seconds
这篇关于定时器的EDITTEXT盒:只有创建视图层次可以触摸其观点原来的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文