TextInputLayout在方向更改Android时显示错误

时间:2017-03-03 10:03:32

标签: android android-textinputlayout android-textinputedittext

我正在使用TextInputLayout和TextInputEditText这样的

public partial class Artikl
{
        [Browsable(false)]
        public int IDArtikli { get; set; }
        public string Barkod { get; set; }
        [DisplayName("Šifra")]
        public Nullable<int> Sifra { get; set; }
        public string Naziv { get; set; }
        [DisplayName("JM")]
        public string JedinicaMjere { get; set; }
        public decimal Tarifa { get; set; }
        [DisplayName("Prodajna")]
        public Nullable<decimal> ProdajnaCijena { get; set; }
        [Browsable(false)]
        public Nullable<bool> Flag { get; set; }
        public Nullable<decimal> Kalo { get; set; }
        [DisplayName("Nabavna")]
        public Nullable<decimal> NabavnaCijena { get; set; }
        [DisplayName("Veleprodajna")]
        public Nullable<decimal> VeleprodajnaCijena { get; set; }
        public Nullable<decimal> Zalihe { get; set; }
 }

风格是

private void txtSifra_TextChanged(object sender, EventArgs e)
{
     var artikl = _UnitOfWork.Artikl.Find(???);
     txtNaziv.Text = artikl.Naziv;
}

Java代码

<android.support.design.widget.TextInputLayout
    android:id="@+id/userName_til"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorTextAppearance="@style/error_appearance">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/etUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/username"
            android:inputType="textEmailAddress"
            android:maxLines="1" />

</android.support.design.widget.TextInputLayout>

我正在使用......

<style name="error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@android:color/holo_red_light</item>
    <item name="android:textSize">12sp</item>
</style>

参考图像肖像 While image in Normal time

景观改变后 Landscape mode error displaying time

On orientation更改自动显示EditText中的错误提示。

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

尝试在TextWatcher中添加Runnable,以便在android恢复编辑文本的状态后附加它。像这样:

tilUserName = (TextInputLayout) findViewById(R.id.userName_til);
etUserName = (TextInputEditText) findViewById(R.id.etUserName);
etUserName.post(new Runnable() {
        @Override
        public void run() {
            etUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Timber.d("OnTextChanged Called");
                if (s.length() < 1) {
                    tilUserName.setErrorEnabled(true);
                    tilUserName.setError(getString(R.string.please_enter_email_id));
                } else if (!AppUtils.isValidEmail(s)) {
                    tilUserName.setErrorEnabled(true);
                    tilUserName.setError(getString(R.string.please_enter_valid_email_id));
                } else {
                    tilUserName.setError(null);
                    tilUserName.setErrorEnabled(false);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
};

引自Android - onTextChanged() called on when phone orientation is changed

答案 1 :(得分:0)

因为在旋转屏幕时调用onTextChange()(可能是:D)

用户点击完成后,您应该检查有效。