如何实现AlertDialog。密码不输入编辑文本?

时间:2011-12-01 13:19:04

标签: android alertdialog

我已经使用edittext和按钮实现了活动:

用户输入密码并单击按钮后,我想验证密码,如果密码正确,请打开另一个活动。如果密码错误,我想使用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);                   
        }   
    });

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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();
    }
 })