gpt4 book ai didi

android - 在 alertDialog 中验证 EditText

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:37 25 4
gpt4 key购买 nike

我正在尝试在 AlertDialog 上的 EditText 上添加空字段验证。但即使字段为空,也不会显示错误消息,而是关闭 AlertDialog。但是,如果条件运行良好,因为如果任何字段为空,我将无法进行后期操作。

这是我的 Java 示例代码:

public class TourActivity extends AppCompatActivity {

private LayoutInflater inflater;
private FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = TourActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_trip, null);
final EditText editEvent = (EditText) content.findViewById(R.id.edTxt_EventName);
final EditText editStartDate = (EditText) content.findViewById(R.id.edTxt_EventSDate);


AlertDialog.Builder builder = new AlertDialog.Builder(TourActivity.this);
builder.setView(content)
.setTitle("Add Event")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

editEvent.setError(null);
editStartDate.setError(null);

boolean cancel = false;
View focusView = null;

if (TextUtils.isEmpty(editEvent.getText().toString()))) {
editEvent.setError("Please Enter Event Name.");
return;
}

if (TextUtils.isEmpty(editStartDate.getText().toString())) {
editStartDate.setError("Please Enter Event Start Date.");
focusView = editStartDate;
cancel = true;
}


if (cancel == true) {
Snackbar.make(findViewById(android.R.id.content),
"Event Unsuccessful.", Snackbar.LENGTH_LONG)
.setActionTextColor(Color.RED)
.show();
focusView.requestFocus();
} else {
// Some action here
}
}
})
.setNegativeButton(cancel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}

最佳答案

此答案编译自Android Dialog, keep dialog open when button is pressed .

写在Dismissing Dialog API Guide ,

When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.

所以你需要制作一个自定义的点击监听器来防止对话框被关闭。

First Way
您可以防止肯定按钮关闭对话框。您基本上需要:

  1. 使用 DialogBu​​ilder 创建对话框
  2. 显示() 对话框
  3. 在显示的对话框中找到按钮并覆盖它们的 onClickListener

因此,创建一个监听器类:

class CustomListener implements View.OnClickListener {
private final Dialog dialog;

public CustomListener(Dialog dialog) {
this.dialog = dialog;
}

@Override
public void onClick(View v) {
// Do whatever you want here

// If you want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}

然后在显示对话框时使用:

AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

请记住,您需要显示对话框,否则将找不到按钮。此外,请务必将 DialogInterface.BUTTON_POSITIVE 更改为您用于添加按钮的任何值。另请注意,在 DialogBu​​ilder 中添加按钮时,您需要提供 onClickListeners - 但您不能在其中添加自定义监听器 - 如果您仍然关闭对话框调用 show() 后不要覆盖监听器。

Second Way

下面是一个使用匿名类的相同方法的示例,因此它全部在一个代码流中:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
AlertDialog dialog = builder.create();
dialog.show();

//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean wantToCloseDialog = false;

//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});

关于android - 在 alertDialog 中验证 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com