使用LinearLayout的AlertDialog不应在按钮单击时关闭(AlertDialog with LinearLayout should not dismiss on button clic

编程入门 行业动态 更新时间:2024-10-27 15:26:33
使用LinearLayout的AlertDialog不应在按钮单击时关闭(AlertDialog with LinearLayout should not dismiss on button click)

我只希望在满足某些条件时(当给出的姓名和姓氏有效时)我的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);

两者都是不同的按钮。

尝试

按钮b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);

alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {

and

Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);

both are different buttons.

try

Button b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);

更多推荐

本文发布于:2023-07-30 08:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应在   单击   按钮   AlertDialog   LinearLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!