簡體   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