how to set an alert box
in my app i have placed three开发者_Python百科 edit boxes with a ok button. Before pressing the ok button the user must enter all the edit text fields, if any of the edit text is been left empty i want to give an alert box.
in the edit text box the field name such as "Name", "Age" should be in diminished and when it is clicked it must get disappeared.
How to do this , pls anyone help me
Check length:
if (edit1.getText().length() > 0 && edit2.getText.length() > 0 && edit3.getText.length() > 0) {
// Do your normal code here
} else {
// Call your alert dialog creation
}
Diminished? You mean a hint (which is shown when there's no text in the field)? This is done like this...
XML inside the EditText field:
android:hint="Clear by clicking"
Source code:
nameEditText.setHint("Clear by clicking");
Remove text on click (if you have already created an EditText field called nameEditText):
// Clear text when clicked
nameEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nameEditText.setText("");
}
});
And then do what Vladimir said
Just use AlertDialog. Check all the conditions and if there is an error build a dialog and show it.
you can design this ui in your activity, and this activity should have the theme android:theme="@android:style/Theme.Dialog"
. And when you want your activity to disappear. simpally call finish()
its simple.. for name checking :
if(editname.getText().tostring().length==0)
show alert...
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("something,");
builder.setMessage("something..");
builder.show():
you can also add button...by
builder.setNeutralButton("name",new DialogInterface.onclick
............}
just try this
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Error Msg).setPositiveButton("OK", alertClickListener).show();
DialogInterface.OnClickListener alertClickListener = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
};
In your Ok Button OnClick do the following
if (et1.getText().toString().length() != 0) {
emailid = String.valueOf(et1.getText());
}
if((emailid==null|| emailid=="")){
tvError.setVisibility(View.VISIBLE);
tvError.setText("All fields are Mandatory");
Toast.makeText(Signin.this,"All fields are Mandatory", Toast.LENGTH_SHORT).show();
}else{
// Your operation
}
where et1 is Edit box 1,emailid is String ..
In your XML file create a textview with option android:visibility="GONE"..
Now in if part Make that textview visible if error occurs or do your process in Else..
Also you can keep toast mesaage...
use this it is best as compared with all validation check
精彩评论