带有前缀的TextInputLayout中的EditText

时间:2018-03-13 06:20:08

标签: android xml

open_files_limit内有一个EditText TextInputLayout +91前缀TextView

EditText

但是当<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin"> <android.support.design.widget.TextInputLayout android:id="@+id/layoutMobile" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/etPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/txt_hint_phone" android:imeOptions="actionDone" android:inputType="phone" android:maxLength="10" android:maxLines="1" android:minLines="1" android:paddingLeft="30dp" android:layout_marginBottom="10dp" android:text="9736625345" android:textColorHint="@color/colorBlack" android:textSize="@dimen/sixteen_sp" /> </android.support.design.widget.TextInputLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerInParent="true" android:text="+91" android:textSize="16sp" /> </RelativeLayout> 被赋予android:paddingLeft="30dp"时,EditText的提示和错误消息也会被转移!!

enter image description here

我希望TextInputLayout的提示和错误消息保持左对齐,而TextInputLayout应该有不可编辑的前缀。

实现此目的的正确方法是什么?

3 个答案:

答案 0 :(得分:0)

尝试使用java: -

   layoutMobile.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) {
                    if (make condition here according to you) {
                        // set error here <layoutMobile.setError();>
                    }
                }

                public void afterTextChanged(Editable edt) {

                }
            });

答案 1 :(得分:0)

如果您不想填充,可以帮助您调整视图。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/activity_vertical_margin">

         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+91"
            android:textSize="16sp" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layoutMobile"
            android:layout_width="0dp"
            android:weight="1"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/etPhoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/txt_hint_phone"
                android:imeOptions="actionDone"
                android:inputType="phone"
                android:maxLength="10"
                android:maxLines="1"
                android:minLines="1"
                android:text="9736625345"
                android:textColorHint="@color/colorBlack"
                android:textSize="@dimen/sixteen_sp" />
        </android.support.design.widget.TextInputLayout>

对于错误指示,请使用此java代码

mEditTextObject.setError("Error!")//to show error
mEditTextObject.setError(null)//to Remove Error

答案 2 :(得分:0)

尝试使用以下代码

设置edittext的选择长度
EditText.setSelection(EditText.getText().length());
相关问题