如何设置可以输入的最大字符数。因此,现在用户可以在alertdialog中输入大量内容,而它应该具有最大值。这与xml不同。这会更容易,这个警报对话框有什么提示吗?
private void RequestNewGroup()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog);
builder.setTitle("Enter Group Name ");
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
builder.setView(groupNameField);
builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
String groupName = groupNameField.getText().toString();
if (TextUtils.isEmpty(groupName))
{
Toast.makeText(MainActivity.this, "Please write Group Name...", Toast.LENGTH_SHORT).show();
}
else
{
CreateNewGroup(groupName);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
}
});
builder.show();
}
1条答案
按热度按时间snz8szmq1#
您需要添加一个
InputFilter
给你EditText
```final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
// ### -> max length
groupNameField.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(###) });