Android 全屏文本输入文本不显示计数器
我正在努力使屏幕与整个屏幕相适应,但也不是没有问题:Android 全屏文本输入文本不显示计数器,android,Android,我正在努力使屏幕与整个屏幕相适应,但也不是没有问题: <?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" xml
<?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>
<>结果是计数器消失,提示出现在中间。如果我更改为包装内容,它将恢复正常行为。例如:
因此,基本上我只需显示计数器即可,将提示与顶部对齐是次要的。我做了测试,但在我的设备上无法正常工作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/edit_text"
android:hint="Type here..."
android:maxLength="1000"
android:gravity="top|left"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
只需尝试重量,而不是匹配父母<代码>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();
您的布局如下所示`
<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>`
`
我会切换到EditText
,而不是textinputtext
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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:endIconMode="none"
app:passwordToggleEnabled="false">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:hint="Type here..."
android:inputType="textPersonName"
android:maxLength="1000" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
只需将android:layout_weight=“1”交给您的文本输入文本即可。这个把戏对我有用
<?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"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:counterEnabled="true"
app:counterMaxLength="1000"
app:counterOverflowTextAppearance="@dimen/_20sdp"
>
<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:layout_weight="1"
android:maxLength="1000"
android:gravity="top"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
希望这对你有帮助
布局:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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>
<TextView
android:id="@+id/txtcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textColor="@color/colorAccent"
android:textSize="10sp"
android:visibility="gone"
android:text="24"/>
</RelativeLayout>
</LinearLayout>
计数器也在edittext中?@Umair尝试过,但sameI问了一个问题,但没有告诉你做什么;)你说的“柜台”是什么意思?你能分享你认为“正常行为”的截图吗?“StaCu没有显示我的设备上的计数器(OnPrand 6 Android 10),而不是仿真器(Android 7.1)<代码> Android:LayOutoSuth=“1”< /Cord> Work,但是你不需要设置<代码> Android:LayOutoHealth=“MatkHyPrime'”的
文本输入文本
而不是包裹以适合整个屏幕
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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>
<TextView
android:id="@+id/txtcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textColor="@color/colorAccent"
android:textSize="10sp"
android:visibility="gone"
android:text="24"/>
</RelativeLayout>
</LinearLayout>
editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
try {
if(StringUtils.isNotEmpty(editText.getText().toString().trim())) {
txtcount.setVisibility(View.VISIBLE);
txtcount.setText(String.valueOf(editText.getText().length()));
}else {
txtcount.setVisibility(View.GONE);
}
} catch (Exception e) {
txtcount.setVisibility(View.GONE);
e.printStackTrace();
}
}
});