2011-12-01 43 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(); 
    } 
}) 
相关问题