简体   繁体   English

EditText中的错误弹出窗口没有显示?

[英]Error-popup in EditText is not showing?

I have an EditTextField with a DatepickerListener on it. 我有一个带有DatepickerListenerEditTextField If I press a submit button it should validate that field and if they are empty it should show an error message. 如果我按下提交按钮,它将验证该字段,如果它们为空,则应显示错误消息。 To display this error message I use the EditText.setError() method but the popup doesnt show. 要显示此错误消息,我使用EditText.setError()方法,但不显示弹出窗口。 I dont know how to fix it. 我不知道如何解决。

I tested it on 2 Devices. 我在2台设备上进行了测试。 Once with a OnePlus 5 on Android 7.1.1 and once on a Samsung Galaxy 7 on Android 7.0.0. 一次在Android 7.1.1上使用OnePlus 5,一次在Android 7.0.0上使用三星Galaxy 7。 Same behaviour on both devices. 两个设备上的行为相同。

Thank you in Advance. 先感谢您。

Here is my Code (Dummy Version): 这是我的代码(虚拟版):

private void initRequestButton(View view) {
    Button button = view.findViewById(R.id.submitButton);
    EditText dateTextField = view.findViewById(R.id.someField);        

    button.setOnClickListener(v -> {
        String dateText = dateTextField.getText().toString();
        if (dateText.isEmpty()) {
                dateTextField.setError("A date is required");
        } else {
            //do Something
        }
    });
}

The Layout 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:errorEnabled="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:layout_marginTop="10dip"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/someField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_marginTop="20dip"
        android:layout_weight="0.5"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:inputType="none" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:layout_marginTop="5dip"
    android:orientation="horizontal">

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.3"
        android:text="@string/someText" />
</LinearLayout>

Just remove the 只需删除

 android:focusable="false"
    android:focusableInTouchMode="false"

properties from the edittext. 编辑文本中的属性。

The code you are using to set error is alright, but the popup pops only when the edittext is focussed either by touching manually or by programmatically, just add, 用来设置错误的代码是可以的,但是仅当通过手动触摸或通过编程方式集中编辑文本时,才弹出弹出窗口,只需添加,

yourEdtext.requestFocus()

after setting error to the field. 将错误设置为字段后。 This will show the popup after validation fails 验证失败后将显示弹出窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM