当用户按下浮动按钮时,我会显示此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();
答案 0 :(得分:2)
在Xml中
将android:maxLines="1"
放在输入xml布局的EditText中。
同样选择InputType
,就像FrédéricLetellier在答案中提到的那样。
例如,android:inputType="phone"
Specifying the Input Method Type
中的更多信息在Java中
input.setMaxLines(1);
input.setInputType(InputType.TYPE_CLASS_PHONE);
在这些示例中,显示了电话号码的设置。您可以在InputType
中查看其他类型 ps:同样在xml中,您可以使用android:singleLine="true"
,但不推荐使用此参数。所以第一个选项听起来更好。
答案 1 :(得分:1)
单独定义行的最大值而不更改inputType是不够的
在你的xml中,使它看起来像单行EditText:
android:maxLines="1"
为防止进入新行:
android:inputType="text"
以编程方式翻译:
input.setMaxLines(1);
input.setInputType(InputType.TYPE_CLASS_TEXT);
答案 2 :(得分:0)
使用EditText的以下属性,即输入视图
android:ellipsize="end"