lis*_*aro 31 java android schedule dialog
我正在开发一个辅助功能应用.当用户想要离开应用程序时,我会显示一个对话框,他必须确认他要离开,如果他在5秒后没有确认对话框应该自动关闭(因为用户可能会意外打开它).这与更改屏幕分辨率时在Windows上发生的情况类似(出现警报,如果您不确认,则会恢复到之前的配置).
这是我显示对话框的方式:
AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
dialog.create().show();
Run Code Online (Sandbox Code Playgroud)
如何在显示后5秒钟关闭对话框?
Vla*_*nov 78
final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
final AlertDialog alert = dialog.create();
alert.show();
// Hide after some seconds
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (alert.isShowing()) {
alert.dismiss();
}
}
};
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.removeCallbacks(runnable);
}
});
handler.postDelayed(runnable, 10000);
Run Code Online (Sandbox Code Playgroud)
moD*_*Dev 19
使用CountDownTimer来实现.
final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
.setTitle("Leaving launcher").setMessage(
"Are you sure you want to leave the launcher?");
dialog.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
exitLauncher();
}
});
final AlertDialog alert = dialog.create();
alert.show();
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
alert.dismiss();
}
}.start();
Run Code Online (Sandbox Code Playgroud)
太晚了,但是我认为这对于在应用程序中使用RxJava的任何人可能都是有用的。
RxJava附带了一个名为的运算符,该运算符.timer()将创建一个Observable,该Observable onNext()在给定的时间段后仅触发一次,然后调用onComplete()。这非常有用,避免了必须创建Handler或Runnable。
可在ReactiveX文档中找到有关此运算符的更多信息。
// Wait afterDelay milliseconds before triggering call
Subscription subscription = Observable
.timer(5000, TimeUnit.MILLISECONDS) // 5000ms = 5s
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
// Remove your AlertDialog here
}
});
Run Code Online (Sandbox Code Playgroud)
您可以通过取消订阅按钮单击上的可观察项来取消计时器触发的行为。因此,如果用户手动关闭警报,请致电subscription.unsubscribe()并取消计时器。
这是代码,请参考此链接:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get button
Button btnShow = (Button)findViewById(R.id.showdialog);
btnShow.setOnClickListener(new View.OnClickListener() {
//on click listener
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("How to close alertdialog programmatically");
builder.setMessage("5 second dialog will close automatically");
builder.setCancelable(true);
final AlertDialog closedialog= builder.create();
closedialog.show();
final Timer timer2 = new Timer();
timer2.schedule(new TimerTask() {
public void run() {
closedialog.dismiss();
timer2.cancel(); //this will cancel the timer of the system
}
}, 5000); // the timer will count 5 seconds....
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
祝您编码愉快!
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.game_message);
game_message = builder.create();
game_message.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
game_message.dismiss(); // when the task active then close the dialog
t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
}
}, 5000);
Run Code Online (Sandbox Code Playgroud)
参考:https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
| 归档时间: |
|
| 查看次数: |
51279 次 |
| 最近记录: |