I am trying to set up a situation where I am waiting for a small period of time say 3 seconds and move on. But if the user clicks my on-screen button then move on as I would have anyway. Both events will trigger the same behaviour namely to update text on the screen. Any thoughts??
我试图建立一种情况,我在等待一小段时间,比如3秒,然后继续前进。但如果用户点击我的屏幕按钮,然后继续我本来应该有的。这两个事件将触发相同的行为,即更新屏幕上的文本。有什么想法?
New to Android but mclovin' it
对安卓来说是全新的,但我不喜欢
Thanks in advance
谢谢提前
2 个解决方案
#1
26
Try something like this:
试试这样:
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;
}
It waits 3 seconds to continue but if the user touches the screen the thread is notified and it stops waiting.
它等待3秒继续,但如果用户触碰屏幕,线程将被通知,它将停止等待。
#2
-3
Try the following,
尝试以下,
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
}
#1
26
Try something like this:
试试这样:
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;
}
It waits 3 seconds to continue but if the user touches the screen the thread is notified and it stops waiting.
它等待3秒继续,但如果用户触碰屏幕,线程将被通知,它将停止等待。
#2
-3
Try the following,
尝试以下,
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
}