【发布时间】:2011-04-26 13:46:55
【问题描述】:
我正在尝试设置一种情况,让我等待一小段时间,比如 3 秒,然后继续前进。但是,如果用户点击我的屏幕按钮,那么我会继续前进。这两个事件将触发相同的行为,即更新屏幕上的文本。有什么想法吗??
Android 新手,但很熟悉它
提前致谢
【问题讨论】:
标签: android
我正在尝试设置一种情况,让我等待一小段时间,比如 3 秒,然后继续前进。但是,如果用户点击我的屏幕按钮,那么我会继续前进。这两个事件将触发相同的行为,即更新屏幕上的文本。有什么想法吗??
Android 新手,但很熟悉它
提前致谢
【问题讨论】:
标签: android
试试这样的:
private Thread thread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutxml);
final MyActivity myActivity = this;
thread= new Thread(){
@Override
public void run(){
try {
synchronized(this){
wait(3000);
}
}
catch(InterruptedException ex){
}
// TODO
}
};
thread.start();
}
@Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(thread){
thread.notifyAll();
}
}
return true;
}
它会等待 3 秒以继续,但如果用户触摸屏幕,则会通知线程并停止等待。
【讨论】:
试试下面的,
button = (Button) findViewById(R.id.buttonView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runnable clickButton = new Runnable() {
@Override
public void run() {
// whatever you would like to implement when or after clicking button
}
};
button.postDelayed(clickButton, 3000); //Delay for 3 seconds to show the result
}
【讨论】: