带前缀的TextInputLayout内部的EditText

3
我有一个位于 TextInputLayout 中的 EditText,并且在 EditText 前面有一个 "+91" 的 TextView 前缀。
<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>

但是,由于EditText被赋予了android:paddingLeft="30dp",导致TextInputLayout的提示和错误消息也被移动了!

enter image description here

我希望TextInputLayout的提示和错误消息保持左对齐,而EditText应该具有不可编辑的前缀。
实现这个的正确方法是什么?

你可以使用TextWatcher来实现这个功能。 - Lucifer
这个回答解决了你的问题吗?在Android中将常量文本放入EditText中,并且该文本不可编辑 - Mahozad
3个回答

2
如果您不想给出填充,也许这个方法可以帮助您调整视图。
<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

0
尝试使用下面的代码为edittext设置选择长度。
EditText.setSelection(EditText.getText().length());

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) {

                }
            });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接