如何处理输入警报对话框中的空文本并控制警报按钮

时间:2019-03-16 14:44:46

标签: android android-edittext android-alertdialog

我有一个alert dialog,我有一个alert dialog的输入。当前的问题是,如果用户未输入任何文本并单击“确定”按钮,我将无法处理来自alert dialog的输入。如果用户未输入任何文本并单击“确定”按钮而不关闭textfield,如何设置alert dialog显示错误。下面是我的代码。

 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Save QR Code");
    builder.setMessage("Please enter the name for the QR Code.");
    builder.setCancelable(true);

    // Set up the input
    final EditText input = new EditText(getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT );
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Here is the part
            m_Text = input.getText().toString().trim();
            if(TextUtils.isEmpty(m_Text)){
                input.setError("Please Enter Name of the Image QR Code");
            }else {
                //do smtg
            }
        }
    });
    builder.show();

2 个答案:

答案 0 :(得分:1)

@Component({ selector: 'app-declaration-type', templateUrl: './declaration-type.component.html', styleUrls: ['./declaration-type.component.scss'] }) export class DeclarationTypeComponent implements OnInit { customCollapsedHeight = '40px'; customExpandedHeight = '40px'; @Input() errorMessage: string; @Input() displayTypes: boolean; @Input() declarationTypes: Declarationtype[]; @Input() badges: Badge[]; @Input() selectedDeclarationType: string; @Input() selectedBadge: Badge; @Input() traderReference: string; @Output() checked = new EventEmitter<boolean>(); @Output() declarationTypeSelected = new EventEmitter<string>(); @Output() badgeSelected = new EventEmitter<Badge>(); @Output() traderReferenceSet = new EventEmitter<string>(); constructor() {} ngOnInit(): void {} checkChanged(value: boolean): void { this.checked.emit(value); } badgeCodeSelected(value: Badge) { this.badgeSelected.emit(value); } onTraderReferenceSet(value: string) { this.traderReferenceSet.emit(value); } } 添加到TextWatcher。当文本为空时,显示错误消息。您甚至可以将EditText中的PositiveButton隐藏起来,以防止用户在输入无效时点击它。

TextWatcher

答案 1 :(得分:1)

您应该稍后设置监听器:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Save QR Code");
    builder.setMessage("Please enter the name for the QR Code.");
    builder.setCancelable(true);
// Set up the input
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT );
builder.setView(input);

builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view1 -> { 
        m_Text = input.getText().toString().trim();
        if(TextUtils.isEmpty(m_Text)){
            input.setError("Please Enter Name of the Image QR Code");
        } else {
            dialog.dismiss();
        }
});

您的方法存在的问题是AlertDialog仍将其侦听器用作您的装饰器。因此它将调用您的侦听器,但随后仍将关闭对话框。 如果要防止出现这种情况,则在显示对话框之后,需要用您的内部监听器覆盖。