我目前正在嘗試保留一個警告對話框,並在屏幕上顯示編輯文本。像往常一樣,有好的和取消按鈕,我想爲流程添加一點錯誤處理。如果用戶在編輯文本框中沒有輸入任何文本並嘗試按下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)
{
}
});
哇,我帶走了駁回呼叫,並通過自身仍然關閉對話框..... – Tastybrownies 2012-08-02 16:11:14