on pressing positive button why dialogbox is closed?
Actually i'm checking some values in my dialog box if they are not valid i don't want to dismiss my dialog box.unfortunately by default When i press submit button my dialog box gets shut down :( how i can escape ???
LayoutInflater factory = LayoutInflater.from(EmailJavaAPI.this);
final View textEntryView = factory.inflate(R.layout.usrpwsdialog,null);
((EditText)textEntryView.findViewById(R.id.etxtUserName)).setText((((EditText)findViewById(R.id.editTextFrom)).getText()).toString());
final EditText usrName = (EditText)textEntryView.findViewById(R.id.etxtUserName);
final EditText usrPws = (EditText)textEntryView.findViewById(R.id.etxtPws);
//AlertDialog alert=new AlertDialog(EmailJavaAPI.this);
return new AlertDialog.Builder(EmailJavaAPI.this)
.setTitle(R.string.kpsdomain)
.setView(textEntryView)
.setPositiveButton(R.string.dgbtnsubmit,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
userName = usrName.getText().toString().trim();
userPws = usrPws.getText().toString().trim();
((EditText)findViewById(R.id.editTextFrom)).setText(userName);
if(userName.equals("")){
textEntryView.findViewById(R.id.txtVWRUserName).setVisibility(View.VISIBLE);
}
if(userPws.equals("")){
textEntryView.findViewById(R.id.txtVWRPws).setVisibility(View.VISIBLE);
}
if(!userName.equals("")&& !userPws.equals("")){
Toast.makeText(EmailJavaAPI.this, "User Name and Password has been set up", Toast.LENGTH_SHORT).show();
}
else{
//***IMPORTANT***
//Dialog Should be opened if this condition is true
}
}
})
.setNegativeButton(R.string.dgbtnDiscard,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
}).create(开发者_JAVA百科);
That's the default behaviour of the alertdialog. You need to customize it like:
@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater inflator = LayoutInflater.from(context);
View view = inflator.inflate(R.layout.yourview, null);
Button positive = (Button)view.findViewById(R.id.btn_positive);
Button negative = (Button)view.findViewById(R.id.btn_negative);
positive.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
removeDialog(0);
}
});
negative.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
removeDialog(0);
}
});
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(view);
return dialog;
}
It will remove dialog when you will call removeDialog(0)
.
You can customize your alert like the below.In that you can give your own instruction(Button Action).When you want to close the dialog you have to call
dialog.dismiss();
AlertDialog dialog = new AlertDialog.Builder(context).create();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.your_layout, null);
Button btn = (Button) layout .findViewById(R.id.btnid);
btnSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
}
);
dialog.setTitle("Alert Title");
dialog.setView(layout);
dialog.show();
精彩评论