1

で問題が発生しsetError()ていEditTextます。アクティビティが開かれると、特定のフィールドが空であるかどうかがチェックされ、trueの場合はエラーメッセージが設定されます。ただし、感嘆符のアイコンは、フィールドにテキストを書き込んでから削除した場合にのみ表示されます。そのフィールドにフォーカスを失うと、アイコンが再び消えます。両方のフィールドNaamTelefonnumerこの検証があります。

Android 2.2.2 SDKを使用しており、アプリケーションは最新のアップデートが適用されたNexus7で実行されています。

私は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には既知のバグがあります。ただし、テストしているNexus7はこれらのバージョンの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

これを試して

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 に答える
0

次のコードを使用できます。

お役に立てば幸いです。

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>"));
于 2013-09-24T06:28:15.327 に答える
0

これは、TextViewで期待されるsetError動作を取得するために必要な唯一の方法です

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
于 2014-07-21T12:35:44.553 に答える