我只希望在满足某些条件时(当给出的姓名和姓氏有效时)我的AlertDialog被解雇 - 否则它应该始终位于父视图的顶部。 我的代码是这样的:
final AlertDialog.Builder alert = new AlertDialog.Builder(this); final TextView instructions = new TextView(this); instructions.setText(R.string.alert_enter_data); final EditText name = new EditText(this); name.setHint(R.string.name); final EditText surname = new EditText(this); surname.setHint(R.string.surname); LinearLayout ll=new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.addView(instructions); ll.addView(name); ll.addView(surname); alert.setView(ll); alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String name_txt = name.getText().toString(); String surname_txt = surname.getText().toString(); if ((name_txt.length() > 1) && (surname_txt.length() > 1)) { dialog.dismiss(); } } }); final AlertDialog alert_dialog = alert.create(); alert_dialog.setCanceledOnTouchOutside(false); alert_dialog.show();使用此代码,无论输入文本如何,按下按钮时AlertDialog消失。 然后我尝试了这个:
alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name_txt = name.getText().toString(); String surname_txt = surname.getText().toString(); String email_txt = email.getText().toString(); String cellphone_txt = cellphone.getText().toString(); String postcode_txt = postcode.getText().toString(); if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) { if (debug_mode) {Log.i(TAG,"clause 1");} String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n"; // upload_to_github(data_to_upload); alert_dialog.dismiss(); } } }); } });但是这样我根本就得不到巴顿。 警报对话框仅包含EditText字段。
I only want my AlertDialog to be dismissed when certain conditions are met (when name and surname given are valid) - else it should always be on top of the parent view. My code is this:
final AlertDialog.Builder alert = new AlertDialog.Builder(this); final TextView instructions = new TextView(this); instructions.setText(R.string.alert_enter_data); final EditText name = new EditText(this); name.setHint(R.string.name); final EditText surname = new EditText(this); surname.setHint(R.string.surname); LinearLayout ll=new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.addView(instructions); ll.addView(name); ll.addView(surname); alert.setView(ll); alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String name_txt = name.getText().toString(); String surname_txt = surname.getText().toString(); if ((name_txt.length() > 1) && (surname_txt.length() > 1)) { dialog.dismiss(); } } }); final AlertDialog alert_dialog = alert.create(); alert_dialog.setCanceledOnTouchOutside(false); alert_dialog.show();With this code the AlertDialog disappears when the button is pressed, regardless of the input text. I then tried this:
alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name_txt = name.getText().toString(); String surname_txt = surname.getText().toString(); String email_txt = email.getText().toString(); String cellphone_txt = cellphone.getText().toString(); String postcode_txt = postcode.getText().toString(); if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) { if (debug_mode) {Log.i(TAG,"clause 1");} String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n"; // upload_to_github(data_to_upload); alert_dialog.dismiss(); } } }); } });But this way I get not Button at all. The Alert Dialog only contains the EditText fields.
最满意答案
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {和
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);两者都是不同的按钮。
尝试
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {按钮b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
and
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);both are different buttons.
try
Button b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
更多推荐
发布评论