0

我正在尝试向用户显示登录对话框(带有自定义视图的自定义对话框),然后单击“登录”时,我正在尝试显示 ProgressDialog,然后在登录完成后将其关闭。

问题是我无法显示 ProgressDialog。

这是我的代码:

    private final MyActivity activity;

    public MyPresenter(MyActivity activity) {
        this.activity = activity;
    }

    public Dialog getLoginSignupDialog() {
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(R.layout.dialog_signup);
        dialog.setTitle("Login");
        final TextView email = (TextView)dialog.findViewById(R.id.textViewEmail);
        final TextView password = (TextView)dialog.findViewById(R.id.textViewPassword);
        Button signupButton = (Button)dialog.findViewById(R.id.buttonSignup);           
        signupButton.setOnClickListener(new OnClickListener() {                 
            public void onClick(View v) {   
                ProgressDialog progressDialog = null;

                try {
                    progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
                    activity.signup(email.getText().toString(), password.getText().toString());
                    progressDialog.dismiss();
                    dialog.dismiss();
                } catch (ParseException e) {
                    if (progressDialog != null) progressDialog.dismiss();
                    Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });     
        return dialog;
    }

这段代码在一个 Presenter 中,它传递了主要的活动实例。只是为了保持 UI 逻辑独立。

在主要活动(onCreate)中,我这样做:

showDialog(DIALOG_SIGNUP);

然后我覆盖onCreateDialog方法:

@Override
protected Dialog onCreateDialog(int id) {   
    Dialog dialog = null;   
    switch (id) {
        case DIALOG_SIGNUP: {
            dialog = presenter.getLoginSignupDialog();                              
            break;
        }
    }   
    return dialog;

}

另一个对话框(自定义登录)显示正常,一切正常,关闭等。但我无法显示 ProgressDialog。

我究竟做错了什么?

4

2 回答 2

0

我认为将您的进度对话框放入您的函数 activity.signup() 并在那里将其关闭会有所帮助。这应该在主 ui 线程中创建对话框。

也许您应该在登录过程中使用 AsyncTask。

于 2012-08-22T03:27:40.100 回答
0

您应该将注册活动作为 AsyncTask 执行,如下所示:

    new AsyncTask<String, Void, Void>() {


        ProgressDialog progressDialog=null;


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                activity.signup(params[0], params[1]);

            } catch (ParseException e) {
                Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null) progressDialog.dismiss();
            dialog.dismiss();
        }
    }.execute(email.getText().toString(), password.getText().toString());
于 2012-08-22T03:48:19.510 回答