gpt4 book ai didi

android - 如何保持显示编辑文本的警报对话框

转载 作者:行者123 更新时间:2023-11-29 18:09:01 25 4
gpt4 key购买 nike

我目前正在尝试在屏幕上显示一个带有编辑文本的警告对话框。和往常一样,有确定和取消按钮,我想在流程中添加一些错误处理。如果用户未在编辑文本框中输入任何文本并尝试按确定,我希望弹出一条 toast 消息,通知他们他们的错误。对话框应该仍然显示,因为如果他们想按确定,他们仍然需要输入一些东西。

这就是我目前所拥有的。顺便说一句,对话框会根据用户从下拉列表中选择的内容弹出。如果您能帮助对话框保持正常运行,我们将不胜感激!!!

这是我的代码:

    regionSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long rowId)
{
regionSelect = regionSpinner.getItemAtPosition(position).toString();
regionInteger = (int) regionSpinner.getItemIdAtPosition(position);




chosenRegion = regionSpinner.getSelectedItem().toString();

if (chosenRegion.equals(ENTER_OPTION))

{
final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);

alert.setTitle(TITLE_DIALOG);

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

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
if(input.getText().toString().length() > 0 && input != null)
{
textValueForEnteredRegion = input.getText().toString();
Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show();


dialog.dismiss();
//added soft keyboard code to make keyboard go away.
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


}
else
{

Toast.makeText(getApplicationContext(), "You must enter a selection.", Toast.LENGTH_SHORT).show();

}
}


});



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

//added soft keyboard code to make keyboard go away.
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


}
});

alert.show();



}


}



@Override
public void onNothingSelected(AdapterView<?> arg0)
{

}


});

最佳答案

重写按钮的 onClick 方法。

参见:Android SDK override AlertDialog events

    AlertDialog.Builder build=new AlertDialog.Builder(this);
build.setMessage("This is a message, dawg.")
.setPositiveButton("Awesomesauce.", new OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//Change stuff in here so that dialog.dismiss() is not called.
//dialog.dismiss(); //closes the window
}
};
AlertDialog myAlert=build.create();
myAlert.show();

编辑:看起来你已经有了这个......你只需要在你的 onPostitiveButton onClick 方法中去掉 dialog.dismiss() ,并在它周围放置一个条件来检查你的 EditText 中是否有一些文本.

关于android - 如何保持显示编辑文本的警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780885/

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