0

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

4

3 回答 3

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);
}
于 2012-04-18T05:50:33.250 回答
-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();
于 2012-04-18T04:47:16.727 回答
-1

尝试使用 Toast 类:

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

于 2012-04-18T03:51:31.893 回答