繁体   English   中英

Android-对话框关闭

[英]Android - Dialog dismiss

我创建了一个简单的登录名,用户可以在其中输入自己的详细信息,并使用AsyncTask将用户输入匹配到SQLite数据库(如果正确)将启动Main活动的意图。

问题:

  1. 如果用户密码/用户名不正确,则加载进度对话框不会关闭,但如果密码/用户名不正确,则显示else声明

在OnClick的Login类中,我声明了一个新的用户LoginTask,输入的是我的AysncTask

    private static class LoginTask extends AsyncTask<Void, Void, Boolean> {

    ProgressDialog pd;
    String username, password;
    private final Context context;
    Intent log;
    // private final WeakReference<Context> reference;

    private LoginTask(Context context, String username, String password) {
        this.context = context;
        this.username = username;
        this.password = password;

        //final Context context = this.context;
        //Controller handler = new Controller(this.context);

        pd = new ProgressDialog(this.context);

    }

    protected void onPreExecute() {
        //super.onPreExecute();
        pd.show(this.context,"Authenticating account ...", "Please wait ...");
        pd.setCanceledOnTouchOutside(false);

    }

    @Override
    protected Boolean doInBackground(Void... p) {
        //final Context context = this.context;
        Controller handler = new Controller(this.context);
        handler.open();
        if (!handler.executeLog(username.trim(), password.trim())){
            return false;
        } else {
            return true;

        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
       // super.onPostExecute(result);
       // final Context context = this.context;
        Controller handler = new Controller(this.context);
        handler.open();

            if (result == false) {

                Toast.makeText(context, "Failed, Incorrect Username/Password", Toast.LENGTH_SHORT).show();

            } else {
                handler.close();
                Intent log = new Intent(this.context, MainActivity.class);
                context.startActivity(log);
                ((Activity)context).finish();
                Toast.makeText(context, "You have successfully logged on, " + username, Toast.LENGTH_LONG).show();
            }
        }
    }

}

改变这个:

protected void onPreExecute() {
      //super.onPreExecute();
       pd.show(this.context,"Authenticating account ...", "Please wait ...");
       pd.setCanceledOnTouchOutside(false);
}

这样:

@Override
protected void onPreExecute() {
    // super.onPreExecute();
    this.pd.setCanceledOnTouchOutside( false );
    this.pd.setCancelable( true );
    this.pd.setTitle( "Authenticating account ..." );
    this.pd.setMessage( "Please wait ..." );
    this.pd.show();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM