3

当用户单击注册按钮时,会出现一个自定义对话框并且用户移动到菜单。但在我的例子中,服装对话框只出现一秒钟,然后用户进入下一个布局。

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

这是我的代码:

    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);

        }
    });
4

2 回答 2

0

执行此类过程的最佳方法是使用AsyncTask

并覆盖onPreExecutedoInBackgroundonPostExecute

官方指南

实现 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);
            }
     });
于 2013-10-04T18:00:15.420 回答
0

有很多不错的选择,包括AlarmManagerTimerTimerTask

您也可以像这样使用处理程序

Handler handler = null;
handler = new Handler(); 
handler.postDelayed(new Runnable(){ 
     public void run(){
         customdialog.cancel();
         customdialog.dismiss();
     }
}, 500);
于 2013-10-04T17:02:11.873 回答