设置自定义对话框的时间

时间:2013-10-04 16:27:22

标签: android timer dialog

当用户登录注册按钮时,会出现一个自定义对话框,用户移动到菜单。但就我而言,Costum对话框只显示一秒钟,然后用户转到下一个布局。

如何设置自定义对话框的时间?

这是我的代码:

    buttonRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            new Thread(new Runnable() {
                @Override
                public void run() {

                    final String name = inputName.getText().toString();
                    final String mail = inputEmail.getText().toString();
                    final String password = inputPassword.getText()
                            .toString();

                    PatientFunctions patientFunction = new PatientFunctions();

                    json = patientFunction.registerPatient(name, mail,
                            password);

                    try {
                        if (json.getString(KEY_SUCCESS) != null) {

                            String res = json.getString(KEY_SUCCESS);
                            if (Integer.parseInt(res) == 1) {
                                Intent main = new Intent(
                                        getApplicationContext(), Main.class);
                                main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(main);
                                finish();
                            }
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            CustomizeDialog customizeDialog = new CustomizeDialog(RegisterPatient.this);
            customizeDialog.show(); 

                            Handler handler = null;
            handler = new Handler(); 
            handler.postDelayed(new Runnable(){ 
                 public void run(){
                     customizeDialog.cancel();
                     customizeDialog.dismiss();
                 }
            }, 3000);

        }
    });

2 个答案:

答案 0 :(得分:0)

有许多不错的选择,包括AlarmManagerTimer& TimerTask

您也可以使用handler

Handler handler = null;
handler = new Handler(); 
handler.postDelayed(new Runnable(){ 
     public void run(){
         customdialog.cancel();
         customdialog.dismiss();
     }
}, 500);

答案 1 :(得分:0)

执行这类流程的最佳方法是使用 AsyncTask

并覆盖 onPreExecute doInBackground onPostExecute

请参阅the official guide

  

实现doInBackground()回调方法,该方法在后台线程池中运行。要更新UI,您应该实现onPostExecute(),它从doInBackground()传递结果并在UI线程中运行,因此您可以安全地更新UI。然后,您可以通过从UI线程

调用execute()来运行该任务
private class Registration extends AsyncTask<String, Void, String>{

    CustomizeDialog customizeDialog;
    @Override
    protected void onPreExecute() {
        // show ur dialog
        customizeDialog = new CustomizeDialog(RegisterPatient.this);
        customizeDialog.show(); 
    }
    @Override
    protected String doInBackground(String... params) {

        final String name = params[0];
        final String email = params[1];
        final String password = params[2];

        PatientFunctions patientFunction = new PatientFunctions();

        json = patientFunction.registerPatient(name, mail,
                password);

        try {
            if (json.getString(KEY_SUCCESS) != null) {

                String res = json.getString(KEY_SUCCESS);
                return res;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onPostExecute(String result) {
        if (Integer.parseInt(res) == 1) {
            if(customizeDialog != null)
                customizeDialog.dismiss();

            Intent main = new Intent(
                    getApplicationContext(), Main.class);
            main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //move to the next activity
            startActivity(main);
            finish();
        }
    }

}

修改

然后按照你想要的那样执行它

buttonRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final String name = inputName.getText().toString();
                final String mail = inputEmail.getText().toString();
                final String password = inputPassword.getText()
                        .toString();
                new Registration().execute(name,mail,password);
            }
     });