如何自动隐藏AlertDialog

时间:2012-04-18 03:49:59

标签: android alertdialog

我的Android应用程序使用  AlertDialog.Builder.setMessage("OK").show(); 但此面板不会自动隐藏。用户必须单击手机上的返回键。 如何显示消息三秒钟,然后自动隐藏它?

3 个答案:

答案 0 :(得分:1)

使用消息处理程序隐藏对话框。试试这个..

AlertDialog test_ok = null;
android.os.Handler messagHandler = null;
Message msg = new Message();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    test_ok = new AlertDialog.Builder(this).setMessage("ok").create();
    test_ok.show();

    messagHandler = new android.os.Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    test_ok.dismiss();
                    break;
            }
        };
    };

    msg.what = 1;
    messagHandler.sendMessageDelayed(msg, 4000);
}

答案 1 :(得分:0)

尝试使用Toast类:

Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

答案 2 :(得分:-1)

Try Thread to show alert for some time....

private boolean mActive = true;
private final int SPLASH_DISPLAY_LENGHT = 2000;

Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (mActive && (waited < SPLASH_DISPLAY_LENGHT)) {
                        sleep(70);

                        waited += 70;

                    }
//set Alert dialog here
                    AlertDialog.Builder.setMessage("OK").show();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        splashTread.start();