2011-12-01 54 views
0

我已經使用edittext和按鈕實現了活動:如何實現AlertDialog。密碼沒有輸入編輯文本?

用戶輸入密碼並單擊按鈕後,我想驗證密碼,如果它是正確的,請打開另一個活動。如果密碼錯誤,我想使用AlertDialog顯示錯誤消息。

可能嗎?怎麼樣?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.enter); 


    //---load the SharedPreferences object--- 
    prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
    String a = prefs.getString(PASSWORD, "pa"); 
    System.out.println("saved Password" +a);  

    EditText et = (EditText)findViewById(R.id.txtName); 
    String theText = et.getText().toString(); 
    System.out.println("entered Password"+theText); 

    //---get the SharedPreferences object--- 
    prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString(PASSWORD, theText); 

    //---save the values--- 
    editor.commit(); 


    Button data = (Button)findViewById(R.id.Ok);    
    data.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent i = new Intent(Enter.this,Data.class);     
      startActivity(i);     
     } 
    }); 
+1

請在這裏發佈一些Java代碼,因爲你發佈的XML和你的問題的描述是混淆 – Cata

回答

1

您可以將密碼字符串捕獲到按鈕單擊事件中。在此,您可以根據密碼的更正打開一個對話框。

像:

EditText txtName=(EditText)findViewById(R.id.txtName); 
Button login=(Button)findViewById(R.id.login); 

login.setOnClickListener(new OnClickListener{ 

     public void onClick() 
     { 
      String password=txtName.getText().toString().trim(); 
      //verify the password and save result to boolean matching; 
      if(matching) 
       //open other activity 
      else 
      { 
      AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
      alt_bld.setMessage("Password is invalid!") 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
         } 
       }) 
     }); 
     AlertDialog alert = alt_bld.create(); 
     // Title for AlertDialog 
     alert.setTitle("Title"); 
     // Icon for AlertDialog 
     alert.setIcon(R.drawable.icon); 
     alert.show(); 
     } 
    } 
}); 
1

你必須移動代碼到onClickListener上的按鈕:

Button data = (Button)findViewById(R.id.Ok);    
data.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     EditText et = (EditText)findViewById(R.id.txtName); 
     String theText = et.getText().toString(); 
     prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
     if (theText.equals(prefs.getString("PASSWORD"))) 
     { 
      Intent i = new Intent(Enter.this,Data.class);     
      startActivity(i);  
     } 
     else 
     { 
      showDialog(myDialogID); 
     }    
    } 
}); 

那麼你當然需要實現AlertDialog。 myDialogID應該是一個唯一的整數。 (只有在使用更多對話框時纔有意義)。請參閱android dev guide for Dialogs

0
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case 0: 
    return new AlertDialog.Builder(this) 
     .setIcon(R.drawable.icon) 
     .setTitle(「This is a dialog with some simple text...」) 
     .setPositiveButton(「OK」, new 
      DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
             int whichButton) 
     { 
     Toast.makeText(getBaseContext(), 
      「OK clicked!」, Toast.LENGTH_SHORT).show(); 
    } 
}) 
     .setNegativeButton(「Cancel」, new 
      DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
             int whichButton) 
     { 
     Toast.makeText(getBaseContext(), 
      「Cancel clicked!」, Toast.LENGTH_SHORT).show(); 
    } 
})