Java 对话框未在Android中显示
我有一些特定的逻辑,需要等待5秒钟才能关闭微调器并显示对话框消息。一切正常,但对话框消息未显示。如果我毫不拖延地做同样的事情,它就会起作用。 我的代码是:Java 对话框未在Android中显示,java,android,android-alertdialog,Java,Android,Android Alertdialog,我有一些特定的逻辑,需要等待5秒钟才能关闭微调器并显示对话框消息。一切正常,但对话框消息未显示。如果我毫不拖延地做同样的事情,它就会起作用。 我的代码是: public static void showMessageNotSentDialog(Activity inActivity) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(inActivity);
public static void showMessageNotSentDialog(Activity inActivity)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(inActivity);
alertDialogBuilder.setTitle(R.string.error);
alertDialogBuilder.setMessage(R.string.error_sending_message);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
showMessageNotSentDialog(inActivity);
}
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}
正在尝试从UI线程显示对话框:
public static void showDelayedErrorMessage(final Activity inActivity)
{
Runnable task = new Runnable() {
public void run() {
com.test.classes.Spinner.hideSpinner();
Runnable messageTask = new Runnable() {
public void run() {
showMessageNotSentDialog(inActivity);
}
};
inActivity.runOnUiThread(messageTask);
}
};
worker.schedule(task, com.test.classes.Spinner.TEXT_SPINNER_HIDEOUT_SEC, TimeUnit.SECONDS);
}
您应该使用,它可以更新gui线程。基本上,您可以在处理程序上延迟后执行,下面是一个示例:
Handler guiHandler = new Handler();
Runnable showDialog = new Runnable(){
public void run(){
//put here the dialog creation
}
}
postDelayed (showDialog ,5000); // Post for 5 seconds
请注意,处理程序的创建必须在GUI线程内,否则将无法工作。请记住,GUI只能由主线程操作。使用倒计时,无需玩线程
new CountDownTimer(5000,5000) {
@Override
public void onTick(long arg0) {
}
@Override
public void onFinish() {
}
}.start();
onfinish在5秒后执行