gpt4 book ai didi

android - 如何使用 Android 在不使用 toast(Alter 对话框)的情况下在设置错误消息中制作编辑文本框?

转载 作者:行者123 更新时间:2023-11-30 01:43:28 25 4
gpt4 key购买 nike

大家好,我在 android 中创建重命名应用程序,我将在设置错误消息中设置一个编辑文本框,使用警告对话框,不使用 toast

示例代码:

        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.rename_title);

folderManager = new FolderManager(getActivity());
folderManager.open();

Cursor c = folderManager.queryAll(itemPos);
if (c.moveToFirst()) {
do {
Newnamefolder = c.getString(1);
} while (c.moveToNext());
}

// Set an EditText view to get user input
final EditText input = new EditText(getActivity());
input.setText(Newnamefolder);
alert.setView(input);
alert.setPositiveButton(R.string.rename_position_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{

Newnamefolder = input.getText().toString();
String Mesage_one = getResources().getString(R.string.folder_already_exit);
String Mesage_two = getResources().getString(R.string.types_minimum_eight_charcter);
String Mesage_three = getResources().getString(R.string.folder_empty);
String Matchnamerename = folderManager.getmatchfoldername(Newnamefolder);

if(Newnamefolder.equals(Matchnamerename))

{

input.setError(Mesage_one);


}
else if(Newnamefolder.length()>12)
{

input.setError(Mesage_two);

}

else if(Newnamefolder.equals(""))
{

input.setError(Mesage_three);

}

else
{
int newfolder = folderManager.update(itemPos,Newnamefolder);
reload();
}

}
});


alert.show();

但问题是一旦在点击OK按钮时Don't show error message to exit the Alert Dialog box...

给我任何解决方案...... friend ?

最佳答案

您可以扩展Dialog 并创建您自己的对话框

public class CustomDialog extends Dialog implements View.OnClickListener {
private boolean success = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
Button positive = (Button) findViewById(R.id.button_positive);
Button negative = (Button) findViewById(R.id.button_negative);
EditText field = (EditText) findViewById(R.id.field);

positive.setOnClickListener(this);
negative.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button_positive:
onPositiveButtonClicked();
break;
case R.id.button_negative:
//onNegativeButtonClicked();
break;
}
}

private void onPositiveButtonClicked() {
if(verifyForm()) {
success = true;
dismiss();
}
}

public boolean isSuccess() {
return success;
}

private boolean verifyForm() {
boolean valid = true;
/* verify each field and setError() if not valid */
if(!TextUtils.isEmpty(field.getText())) { //or any other condition
valid = false;
field.setError("error message");
}
return valid;
}
}

您可以像这样显示您的CustomDialog

final CustomDialog customDialog = new CustomDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if(customDialog.isSuccess()) {
//update your folder manager
}
}
}
customDialog.show();

关于android - 如何使用 Android 在不使用 toast(Alter 对话框)的情况下在设置错误消息中制作编辑文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102944/

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