gpt4 book ai didi

java - 我希望我的警报对话框能够触发并保持显示错误消息

转载 作者:行者123 更新时间:2023-11-30 05:19:35 25 4
gpt4 key购买 nike

如果用户将名称字段留空并单击“确定”,我希望我的警报对话框触发并保持显示错误消息,但即使用户未填写任何内容并单击“确定”,我的对话框也会消失。这是我的代码。请建议我需要进行的更正。

    save2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final dbmanager db= new dbmanager(cgpa3.this);

final AlertDialog.Builder alert = new AlertDialog.Builder(cgpa3.this);

// alert.setTitle("Enter a name");
alert.setMessage("Enter student Name");

// Set an EditText view to get user input
final EditText input = new EditText(cgpa3.this);
alert.setView(input);


alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if(value.isEmpty()){
Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
input.startAnimation(shake);
input.setError("Please enter student name");

}
else
{db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);

}

}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();

};

});

最佳答案

使用这个,

       final EditText editText;

final AlertDialog.Builder alert = new AlertDialog.Builder(DemoActivity.this);
alert.setTitle("Enter a name");
alert.setMessage("Enter student Name");
alert.setCancelable(false);


editText = new EditText(DemoActivity.this);
alert.setView(editText);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

}
});

final AlertDialog dialogs = alert.create();
dialogs.show();

dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String value = editText.getText().toString();
if (value.isEmpty()) {
editText.setError("Please enter student name");
}
else{
dialogs.dismiss();
}

}
});

dialogs.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

dialogs.dismiss();
}
});

关于java - 我希望我的警报对话框能够触发并保持显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785501/

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