2012-02-17 37 views
0

我正在與AlertDialog用戶登錄到應用程序。我已經顯示了兩個編輯視圖,用於輸入用戶名和密碼,並且我還在警報對話框上使用了正面按鈕和負面按鈕。當用戶輸入無效的用戶名或密碼時,則點擊確定我正在用TexView在同一個警報對話框上顯示一個文本,但警報對話框正在關閉。如何保持警報對話框直到用戶在編輯文本域中輸入有效的詳細信息?如何在用戶輸入無效值時返回相同的警報對話框,然後單擊確定?

我已經實現了讓警報如下代碼:

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Login here"); 
    alert.setView(table); 
    alert.setPositiveButton("Login", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 

     objLogin.setLoginUsrName(userNameText.getText().toString()); 
     objLogin.setLoginPassword(passwordText.getText().toString()); 

     if(objLogin.userValid()) 
     { 

      //getting next screen 


     } 
     else{ 
      invalid.setText("Invalid user name or password"); 
      invalid.setTextColor(Color.RED); 

      //diaplay same alert here with invalid text 

     } 

     } 
    }); 

    alert.setNegativeButton("Register", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

       //get register screen code here 
     } 
    }); 

    alert.show();    

我怎麼能顯示警報,直至用戶輸入有效的細節?

回答

1

我認爲問題是AlertDialog類中的positiveButton和negativeButton。 AlertDialog在按下後關閉。用Dialog Builder代替AlertDialog可能會更好,但我沒有測試過。

另一個修復:

嘗試增加兩款Android小工具,你的「表」的佈局,並在其上設置的onclicklistener。做到這一點,而不是AlertDialog的兩個默認正面/負面按鈕。這種方法也將允許您修改按鈕的位置和樣式。

+0

創建一個自定義對話框,而是如何關閉警告對話框,當用戶是有效的? – 2012-02-17 04:07:46

+0

在onClick()方法內調用'alert.dismiss();'。你將不得不提醒''最後'變量,但這應該是好的。 – edthethird 2012-02-17 04:46:12

0

您需要爲AlertDialog創建一個包含兩個按鈕的自定義視圖,並將這些按鈕用於正面和負面。

但是,如果用戶未登錄,則撥打startActivityForResult進行登錄活動會更好。然後在onActivityResult中,如果用戶沒有放入任何內容(選擇取消或按向後鍵),則只需完成第一個啓動的活動。而在你所做的登錄活動中,按鈕按鈕將是相同的,如果登錄信息正確,那麼你可以撥打finish

我曾在我的應用程序中使用AlertDialog進行登錄。我現在使用我上面提到的方法,而且我喜歡它更好。

1

使用下面的佈局

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout android:id="@+id/tableLayout1" android:gravity="center" 
    android:layout_width="200dip" android:layout_height="150dip" 
    xmlns:android="http://schemas.android.com/apk/res/android" > 
    <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:gravity="center" > 

     <EditText android:text="" android:id="@+id/edusername" 
      android:layout_width="80dip" android:layout_height="50dip" android:inputType="textPassword" 
      android:singleLine="true" ></EditText> 
    </TableRow> 
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:gravity="center" > 

     <EditText android:text="" android:id="@+id/edconfirmpwd" 
      android:layout_width="80dip" android:layout_height="50dip" android:inputType="textPassword" 
      android:singleLine="true" ></EditText> 
    </TableRow> 

    <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:gravity="center" > 

     <Button 
      android:id="@+id/ok" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="15dip" 
      android:padding="20dip" 
      android:text="@string/BtnOk" > 
</Button> 

     <Button 
      android:id="@+id/cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="15dip" 
      android:padding="20dip" 
      android:text="@string/cancelBtn" > 
</Button> 

    </TableRow> 


</TableLayout> 

在活動中使用

final Dialog password = new Dialog(activity.this); 
     password.setContentView(R.layout.dialog); 
     password.setTitle("Enter your login ids"); 
       final EditText getUsername = (EditText)password.findViewById(R.id.edUsername); 
     final EditText getPassword = (EditText) password 
       .findViewById(R.id.edconfirmpwd); 
     Button okButton = (Button) password.findViewById(R.id.ok); 
     okButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String password= getPassword.getText().toString(); 
String username = getUsername.getText().toString(); 

       if (password.equals("yourText")&& username.equals("yourtext")) { 
password.dismiss(); 

        } 
       } else { 
        Toast.makeText(getBaseContext(), "InvalidPassword", 
          Toast.LENGTH_SHORT).show(); 
password.show(); 

       } 





      } 
     }); 
     Button cancelButton = (Button) password.findViewById(R.id.cancel); 
     cancelButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       System.out.println("Entered on cancel button clicked"); 

       password.dismiss(); 

      } 
     }); 
     password.show(); 

    } 
相關問題