EditText处于非活动状态时如何设置提示位置

时间:2020-03-01 19:55:22

标签: android xamarin.android

当我在EditText中调整了一些无效的值时,如下图所示: enter image description here

当我从非活动状态的EditText中删除一个值时,它看起来像这样:

enter image description here

是否可以设置TextInputLayer或TextInputEditText的某些属性以使其处于非活动状态,例如: enter image description here

2 个答案:

答案 0 :(得分:1)

我认为这是TextInputEditTextTextInputLayout的默认行为,可以直接设置属性,但是您可以通过一些设置来解决此问题:

您可以向其中添加FocusChange事件,当它不清晰时,为其设置一个" "(空值):

TextInputEditText textInputEditText = FindViewById<TextInputEditText>(Resource.Id.editText);

textInputEditText.FocusChange += delegate
         {
             if (!textInputEditText.IsFocused)
             {
                 if (string.IsNullOrEmpty( textInputEditText.Text))
                 {
                     textInputEditText.Text = " ";
                 }
             }
             else
             {
                 if (textInputEditText.Text == " ")
                 {
                     textInputEditText.Text = "";
                 }
             }
         };

答案 1 :(得分:0)

根据文档,您应该按如下所示将编辑文本包装在TextInputLayout中:

 <com.google.android.material.textfield.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/form_username">

     <com.google.android.material.textfield.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

 </com.google.android.material.textfield.TextInputLayout>

还要确保在应用的gradle构建文件中具有材质设计库。希望这会有所帮助:)