安卓的EditText SETERROR()不能按预期工作(Android EditText set

2019-08-02 20:18发布

我有一个问题setError()EditText 。 当活动被打开,它会检查,如果某些字段为空,并设置对他们的错误消息如果属实。 然而,如果我写在外地一些文本只显示感叹号图标,然后将其删除。 如果我失去了专注于这一领域,图标会再次消失。 这两个领域NaamTelefonnumer有此验证。

我使用Android 2.2.2 SDK和应用程序为最新版本在Nexus 7上运行。

我的Util类:

public class Util {

    private static String TAG = "Util Class";

    public static boolean editTextIsEmpty(EditText edittext) {
        if (edittext.getText().toString().trim().length() < 1)
            return true;
        else
            return false;
    }

    public void editTextListener(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (editTextIsEmpty(editText) && editText.isEnabled())
                    editText.setError("Nodig");
                else
                    editText.setError(null);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (editTextIsEmpty(editText) && editText.isEnabled())
                    editText.setError("Nodig");
                else
                    editText.setError(null);
            }
        });
    }
}

然后我有方法validateInput()在我的活动:

public class DeliveryActivity extends BaseActivity {

    private ImageButton btnSetDate;

    private Button btnToSummary;
    private Button btnSearchAddress;

    private EditText txtPostcode;
    private EditText txtHouseNumber;
    private EditText txtHouseNumberSuffix;
    private EditText txtStreet;
    private EditText txtCity;
    private EditText txtDeliveryDate;
    private EditText txtName;
    private EditText txtPhone;
    private EditText txtEmail;
    private EditText txtRemark;

    private TextView lblExtraDeliveryInfo;

    private Spinner spinnerDelivery;
    private Spinner spinnerDeliveryPeriod;
    private Spinner spinnerContact;
    private Spinner spinnerDeliveryAddress;
    private Spinner spinnerExtraDeliveryInfo;

    private RelativeLayout rlDeliveryAddressDetails;

    private DevRestHelper additionalDeliveryInfo;
    private DevRestHelper searchClientAddress;

    private Util util = new Util();

    private int year;
    private int month;
    private int day;

    public static final int DIALOG_DATEPICKER = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivery);

        initControls();

        validateInput();
    }

    private void initControls() {
        btnSetDate = (ImageButton) findViewById(R.id.activity_delivery_btnCalendar);

        btnToSummary = (Button) findViewById(R.id.activity_delivery_btnSummary);
        btnSearchAddress = (Button) findViewById(R.id.activity_delivery_btnSearchAddress);

        spinnerDelivery = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryMethod);
        spinnerDeliveryPeriod = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryPeriod);
        spinnerContact = (Spinner) findViewById(R.id.activity_delivery_spinnerContactperson);
        spinnerDeliveryAddress = (Spinner) findViewById(R.id.activity_delivery_spinnerDeliveryAddress);
        spinnerExtraDeliveryInfo = (Spinner) findViewById(R.id.activity_delivery_spinnerExtraDeliveryInformation);

        txtPostcode = (EditText) findViewById(R.id.activity_delivery_txtPostcode);
        txtHouseNumber = (EditText) findViewById(R.id.activity_delivery_txtHousenumber);
        txtHouseNumberSuffix = (EditText) findViewById(R.id.activity_delivery_txtHousenumberSuffix);
        txtStreet = (EditText) findViewById(R.id.activity_delivery_txtStreet);
        txtCity = (EditText) findViewById(R.id.activity_delivery_txtCity);
        txtDeliveryDate = (EditText) findViewById(R.id.activity_delivery_txtDeliveryDate);
        txtName = (EditText) findViewById(R.id.activity_delivery_txtName);
        txtPhone = (EditText) findViewById(R.id.activity_delivery_txtPhone);
        txtEmail = (EditText) findViewById(R.id.activity_delivery_txtEmail);
        txtRemark = (EditText) findViewById(R.id.activity_delivery_txtRemark);

        lblExtraDeliveryInfo = (TextView) findViewById(R.id.activity_delivery_lblExtraDetailInformation);

        rlDeliveryAddressDetails = (RelativeLayout) findViewById(R.id.activity_delivery_rlDeliveryAddressDetails);
    }

    private void validateInput() {
        util.editTextListener(txtPostcode);
        util.editTextListener(txtHouseNumber);
        util.editTextListener(txtDeliveryDate);
    }
}

我只想说在蓝叠仿真代码工作。

Answer 1:

有与果冻Bean_MR1(4.2和4.2.1)SETERROR一个已知的bug。 但是我假设了Nexus 7,你与运行的是Android的这些版本的一个测试。 在这里看到: http://code.google.com/p/android/issues/detail?id=40417

将显示错误,而你必须关注那些领域的EditText,但是当你失去焦点,错误图标是不可见的通知问题的用户。



Answer 2:

在你的任何视图或编辑文本设置错误,只需调用

yourEditText.requestFocus();
yourEditText.setError("Your Error Message");

然后设置错误。 这将解决您的问题。 ATLEAST矿做了。



Answer 3:

试试这个

new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        if (editTextIsEmpty(editText) && editText.isEnabled())
            editText.setError("Nodig");
        else
            editText.setError(null);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // nothing here
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // nothing here
    }

}


Answer 4:

您可以使用下面的代码:

也许这将是对你有所帮助:

mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                    com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);

但是,您可以设置一个跨区使用重载的自定义错误图标setError(CharSequence, Drawable)

您可以轻松地创建使用HTML从跨区fromHtml()

例如:

yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));


Answer 5:

这是你唯一需要的得到预期TextView的SETERROR行为

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"


文章来源: Android EditText setError() doesn't work as expected