我有這個AlertDialog顯示當用戶按下浮動按鈕。我不希望用戶能夠插入多行輸入(即如果她試圖按下回車鍵,則忽略她)。我該怎麼做呢?這裏是我的代碼:如何在警報對話框中強制輸入單行?
final EditText input = new EditText(getActivity());
new AlertDialog.Builder(getActivity()).setTitle("Package creation").setMessage("Insert package name:").setView(input).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
CharSequence toastText;
if (input.length() > 0) {
/* save the package name in the database */
toastText = getString(R.string.toast_package_saved);
} else toastText = getString(R.string.toast_empty_text);
Toast toast = Toast.makeText(getActivity(), toastText, Toast.LENGTH_SHORT);
toast.show();
input.getText().clear();
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do nothing
}
}).show();
輸入視圖是一個EditText? – androidevil
是的。對不起:我忘了在代碼中添加該行!我會立即修復它。 – Robb1