按下正按钮为什么对话框关闭?

时间:2011-09-09 09:38:59

标签: android

其实我正在检查我的对话框中的一些值,如果它们无效我不想解雇我的对话框。很遗憾,默认情况下,当我按下提交按钮时,我的对话框被关闭:(我怎么能逃脱???

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();

2 个答案:

答案 0 :(得分:1)

这是alertdialog的默认行为。您需要自定义它:

@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;
}

当您拨打removeDialog(0)时,它会删除对话框。

答案 1 :(得分:0)

您可以像下面一样自定义警报。您可以自己指示(按钮操作)。当您要关闭对话框时,您必须致电

  

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();