Reputation: 8645
I have implemented activity with edittext and button:
After user entered a password and clicked a button, I'd like to verify the password and if it is correct open another activity. If the password is wrong I'd like to display an error message using AlertDialog.
Is it possible? How?
@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);
}
});
Upvotes: 0
Views: 1315
Reputation: 8645
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();
}
})
Upvotes: 0
Reputation: 19250
You can catch the password string into button click event.There,you can open a dialog depending on the correction of password.
Like:
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();
}
}
});
Upvotes: 1
Reputation: 10345
You have to move code into the onClickListener on your button:
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);
}
}
});
Then of course you need to implement the AlertDialog. myDialogID
should be an unique integer. (Only relevant if you use more Dialogs). See the android dev guide for Dialogs.
Upvotes: 1