我目前正在尝试保持一个警报对话框,并在屏幕上显示编辑文本。像往常一样,有“确定”和“取消”按钮,我想在该过程中添加一些错误处理。如果用户没有在编辑文本框中输入任何文本并尝试按 OK,我希望弹出一条 toast 消息,通知他们他们的错误。对话框应该仍然显示,因为如果他们想按 OK,仍然需要输入一些内容。
这就是我到目前为止所拥有的。顺便说一句,对话框会根据用户从下拉列表中选择的内容弹出。任何帮助让对话框保持不变将不胜感激!!!
这是我的代码:
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)
{
}
});