【发布时间】:2013-01-03 01:11:37
【问题描述】:
这是我的对话框代码,如果编辑文本中的文本大小大于 5,我想禁用肯定按钮,如果大小
private void myDialog(String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.new_dialog, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(dialogView);
final EditText dialogEdittext = (EditText) dialogView.findViewById(R.id.dialog_editText);
final TextView dialogMessage = (TextView) dialogView.findViewById(R.id.dialog_limit);
dialogEdittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
// if text length is greater than 5 disable positive button
// else enable
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
final Dialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
}
【问题讨论】:
-
我已经看过这个链接,它只会在创建对话框时禁用/启用按钮一次,但每次用户在edittext中输入字符时我都希望这样做
-
我从不这样做。我已经使用带有按钮的自定义对话框进行了tihs,并在其中检查了验证。你可以这样做。试试看