我正在尝试使TextInputEditText适合整个屏幕,但并非没有问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:counterEnabled="true"
app:counterMaxLength="1000"
app:passwordToggleEnabled="false"
app:endIconMode="none">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text"
android:hint="Type here..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="1000"
android:gravity="top"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
输入类型:
TextInputEditText tv = findViewById( R.id.edit_text );
tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );
Run Code Online (Sandbox Code Playgroud)
结果是计数器不见了,提示出现在中间。如果我更改为包装内容,它将恢复正常行为。例子:
所以基本上我会满足于只显示计数器,将提示对齐到顶部是次要的。
只需尝试重量而不是 match_parent。 android:layout_weight="1"
计数器将可见。
当我们点击 EditText 或 EditText 处于焦点时,提示对齐是可以的。由于 EditText 处于全屏请求焦点将解决提示对齐问题。
TextInputEditText tv = findViewById( R.id.edit_text );
tv.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE );
tv.requestFocus();
Run Code Online (Sandbox Code Playgroud)
您的布局将如下`
<LinearLayout
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:counterEnabled="true"
app:counterMaxLength="1000"
app:counterTextColor="@color/black"
app:passwordToggleEnabled="false"
app:endIconMode="none">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text"
android:hint="Type here..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLength="1000"
android:gravity="top"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
735 次 |
| 最近记录: |