Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带前缀的TextInputLayout中的EditText_Android_Xml - Fatal编程技术网

Android 带前缀的TextInputLayout中的EditText

Android 带前缀的TextInputLayout中的EditText,android,xml,Android,Xml,我在文本输入布局中有一个编辑文本,前缀为编辑文本 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin"> <android.support.d

我在
文本输入布局
中有一个
编辑文本
,前缀为
编辑文本

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

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


实现这一点的正确方法是什么?

尝试使用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) {

                }
            });
layoutMobile.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
如果(根据您的要求在此处设定条件){
//在这里设置错误
}
}
更改后的公共无效(可编辑edt){
}
});

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

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

尝试使用下面的代码设置编辑文本的选择长度

EditText.setSelection(EditText.getText().length());

你可以用a来回答这个问题吗?
EditText.setSelection(EditText.getText().length());