Edw*_*mes 3 xml android android-edittext android-alertdialog
上图显示了一个编辑文本,其文本限制显示在右下角,长度为 250 个字符。当用户在 editText 中键入字符时,“250”会减少。
我一直在尝试将其实现到我的编辑文本中,但这有点具有挑战性。我尝试过各种东西。只是为了让您知道我正在使用alertDialog,其中包含editText。然后,alertDialog 显示一个 xml,其中包含 editText 属性。
谢谢您的帮助!!!!
这对于 Android 材料来说非常简单。我希望它有帮助:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true" --this makes counter enable
app:counterMaxLength="20" -- this is the limittion
android:hint="@string/label">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想要一个 EditeText,您可以使用 TextWatcher:
private TextView mTextView;
private EditText mEditText;
mTextView.setText("250");
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
mTextView.setText(String.valueOf(250-s.length()));
}
public void afterTextChanged(Editable s) {
}
};
mEditText.addTextChangedListener(mTextEditorWatcher);
Run Code Online (Sandbox Code Playgroud)