1

我在使用时遇到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);
    }
}

我只想说代码在 BlueStacks 模拟器上工作。

在此处输入图像描述

4

5 回答 5

3

Jelly Bean_MR1(4.2 和 4.2.1)上的 setError 存在一个已知错误。但是,我假设您正在测试的 Nexus 7 正在运行这些版本的 Android 之一。见这里:http ://code.google.com/p/android/issues/detail?id=40417

当您专注于该 EditText 字段时,将显示错误,但当您失去焦点时,错误图标将不可见以通知用户该问题。

于 2012-12-16T12:33:56.723 回答
3

在任何视图或编辑文本上设置错误之前,只需调用

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

然后设置错误。它会解决你的问题。至少我的做到了。

于 2017-04-07T06:49:45.247 回答
0

这是您唯一需要在 TextView 上获得预期的 setError 行为

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
于 2014-07-21T12:35:44.553 回答
0

您可以使用以下代码:

希望对您有所帮助:

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

但是,您可以使用重载的setError(CharSequence, Drawable).

您可以使用fromHtml().

例如:

yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));
于 2013-09-24T06:28:15.327 回答
0

尝试这个

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
    }

}
于 2012-11-16T10:19:44.763 回答