我如何在 UI 中显示字符限制以及为用户数字化了多少? Android Java
How can I show in UI the limit of character and how much was digited for the user? Android Java
我该怎么做?
我的意思是显示字符限制以及输入文本中有多少数字。
您可以使用 TextInputLayout 来实现:
app:counterEnabled="true"
app:counterMaxLength="250"
例子
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="250">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="250" />
</android.support.design.widget.TextInputLayout>
实现它看起来像这样:
private TextView mTextView;
private EditText mEditText;
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(s.length()));
}
public void afterTextChanged(Editable s) {
}
};
然后您必须将编辑文本设置为侦听器:
mEditText.addTextChangedListener(mTextEditorWatcher);
然后在布局中将您的编辑文本包裹在以下 TextInputLayout 中:
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="20"
>
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text Hint"
/>
</android.support.design.widget.TextInputLayout>
祝你好运:)
您只需在 xml、
中使用 TextInputLayout
小部件即可完成此任务
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="50">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="50" />
</android.support.design.widget.TextInputLayout>
这里counterEnabled
是开启计数器,counterMaxLength
设置计数器的最大量程
虽然 maxLength
在 EditText
中设置了最大字符数,但可以在您的 EditText 中输入。
你可以TextInputLayout which is support character counting functionality. Here, I have mentioned full description on how to implement TextInputLayout with TextInputEditText
字符计数器
Character Counter is a feature used by quite a few applications.
(Remember Twitter character limit?). Set app:counterEnabled to true
and app:counterMaxLength with the maximum number of characters you
want in the TextInputLayout. Character Counter is by default displayed
below the EditText (bottom-right) and while writing this tutorial,
there’s no way to change the position, yet. Styling the counter is
similar to styling the hint text. app:counterTextAppearance is the
attribute used this time. We’ve added the following style inside the
styles.xml file in our project.
<style name="CounterText" parent="TextAppearance.Design.Counter">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/my_pink</item>
</style>
下面的 xml 代码来自 activity_main.xml 布局,并且具有带有默认字符计数器和自定义字符计数器的 EditText 字段。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
app:counterEnabled="true"
app:counterMaxLength="5"
app:hintTextAppearance="@style/HintText">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Character Counter Limit 10" />
</android.support.design.widget.TextInputLayout>
上面代码的输出如下。
我该怎么做? 我的意思是显示字符限制以及输入文本中有多少数字。
您可以使用 TextInputLayout 来实现:
app:counterEnabled="true"
app:counterMaxLength="250"
例子
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="250">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="250" />
</android.support.design.widget.TextInputLayout>
实现它看起来像这样:
private TextView mTextView;
private EditText mEditText;
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(s.length()));
}
public void afterTextChanged(Editable s) {
}
};
然后您必须将编辑文本设置为侦听器:
mEditText.addTextChangedListener(mTextEditorWatcher);
然后在布局中将您的编辑文本包裹在以下 TextInputLayout 中:
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="20"
>
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text Hint"
/>
</android.support.design.widget.TextInputLayout>
祝你好运:)
您只需在 xml、
中使用TextInputLayout
小部件即可完成此任务
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="50">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="50" />
</android.support.design.widget.TextInputLayout>
这里counterEnabled
是开启计数器,counterMaxLength
设置计数器的最大量程
虽然 maxLength
在 EditText
中设置了最大字符数,但可以在您的 EditText 中输入。
你可以TextInputLayout which is support character counting functionality. Here, I have mentioned full description on how to implement TextInputLayout with TextInputEditText
字符计数器
Character Counter is a feature used by quite a few applications. (Remember Twitter character limit?). Set app:counterEnabled to true and app:counterMaxLength with the maximum number of characters you want in the TextInputLayout. Character Counter is by default displayed below the EditText (bottom-right) and while writing this tutorial, there’s no way to change the position, yet. Styling the counter is similar to styling the hint text. app:counterTextAppearance is the attribute used this time. We’ve added the following style inside the styles.xml file in our project.
<style name="CounterText" parent="TextAppearance.Design.Counter">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/my_pink</item>
</style>
下面的 xml 代码来自 activity_main.xml 布局,并且具有带有默认字符计数器和自定义字符计数器的 EditText 字段。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
app:counterEnabled="true"
app:counterMaxLength="5"
app:hintTextAppearance="@style/HintText">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Character Counter Limit 10" />
</android.support.design.widget.TextInputLayout>
上面代码的输出如下。