使文本计数器始终保持可见
Make Text Counter Always Stay Visible
问题:字符计数器在屏幕外运行
我发现很难有一个字符计数器,一旦输入文本滚动超过一个屏幕尺寸就不会消失。
这是我在布局中的内容 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android.support.design="http://schemas.android.com/tools">
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
android:overScrollMode="never"
android.support.design:errorEnabled="true"
android.support.design:hintEnabled="true"
app:counterMaxLength="@integer/global_comments_size">
<android.support.design.widget.TextInputEditText
android:id="@+id/action_global_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginTop="8dp"
android:hint="@string/edit_comments"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:maxLength="@integer/global_comments_size"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
counter visible until scroll leaves the screen
Counter becomes invisible as the text scroll over the screen
可能的选项:
我认为以下三个选项是可能的解决方案。
制作底部工具栏并以编程方式添加计数器
一旦达到最大大小,以编程方式弹出显示
以某种方式在 xml 中配置 TextInputLayout 或使用其他库。
我更喜欢选项 3。但是,我现在不知所措,既没有在文档中也没有通过谷歌搜索论坛找到任何解决方案或提示。
已解决
找到解决方案
阅读更多内容后,我发现已经在 Whosebug here 上提出了关于计数器 运行 的问题,并提供了解决方案建议。
但是,该解决方案并不像预期的那样使用简单的 xml 条目,而是必须以编程方式完成
我根据从 here 和 此处 .
收集的信息发布带有源代码的解决方案
你所要做的就是像这样使用alignParent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.design="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/action_global_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginTop="8dp"
android:hint="@string/edit_comments"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:maxLength="@integer/global_comments_size"
android:layout_alignParentTop="true"
android:layout_above="@+id/textContainer"
android:scrollbars="vertical" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:overScrollMode="never"
android.support.design:errorEnabled="true"
android.support.design:hintEnabled="true"
app:counterEnabled="true"
app:counterMaxLength="@integer/global_comments_size"/></RelativeLayout>
我通过阅读文档和 Whosebug 找到了解决方案。
我在这里发布了一个完整的示例,以便更容易地重现该行为。
不幸的是,似乎没有一种方法可以简单地配置一个 xml 属性 让计数器停留在屏幕的可见区域。
解决方案在于使用接口 class TextWatchers,幸运的是它只需要几行代码。
1)创建res/values/strings.xml文件来定义最大字符大小
<integer name="max_comment_size">50</integer>
2) 在你的 activity 中添加以下两个视图
即res/layout/activity_main.xml
我喜欢把柜台放在最上面。如果您更喜欢将 TextView 和 TextEdit 放在底部,则可能需要交换它。
<TextView
android:id="@+id/constraint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remaining characters/Maximum characters: --/--" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:maxLength="@integer/max_comment_size" />
请注意,textMultiLine 不限制 EditText 字段的最大行数。它只限制布局大小。
使用 maxLength,您可以指定允许的最大字符数。
3)在java文件中使用TextWatcher统计输入的字符数并显示在TextView中
Java 文件在我的示例中是:java/com/example/mycompany/textcounter/MainActivity.java
接口class TextWatcher 有三个方法需要实现。但是,出于此目的,我们只对 onTextChange() 感兴趣。
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private EditText mEditText;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } //nothing to be done here
public void onTextChanged(CharSequence s, int start, int before, int count) {
Integer max_comment = getResources().getInteger(R.integer.max_comment_size);
mTextView.setText("Remaining characters" + "/Maximum characters: "+ String.valueOf(max_comment-s.length()) +"/" + max_comment);
}
public void afterTextChanged(Editable s) { } //nothing to be done here
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views
mEditText = (EditText) findViewById(R.id.editText);
mTextView = (TextView) findViewById(R.id.constraint_textview);
mEditText.addTextChangedListener(mTextEditorWatcher);
}
}
进一步说明:
解决方案基于提供的信息
here 关于计数器 运行 关闭屏幕和
here 关于如何使用 TextWatcher
也请参阅 android 关于 TextWatcher and TextEdit 的文档。
我用 maxLength 限制了 TextEdit 字段中输入字符的最大数量。
如果您确实需要限制最大行数,请参阅讨论和可能的解决方案
here and here 或使用 Whosebug 上的搜索。
问题:字符计数器在屏幕外运行
我发现很难有一个字符计数器,一旦输入文本滚动超过一个屏幕尺寸就不会消失。
这是我在布局中的内容 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android.support.design="http://schemas.android.com/tools">
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
android:overScrollMode="never"
android.support.design:errorEnabled="true"
android.support.design:hintEnabled="true"
app:counterMaxLength="@integer/global_comments_size">
<android.support.design.widget.TextInputEditText
android:id="@+id/action_global_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginTop="8dp"
android:hint="@string/edit_comments"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:maxLength="@integer/global_comments_size"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
counter visible until scroll leaves the screen
Counter becomes invisible as the text scroll over the screen
可能的选项:
我认为以下三个选项是可能的解决方案。
制作底部工具栏并以编程方式添加计数器
一旦达到最大大小,以编程方式弹出显示
以某种方式在 xml 中配置 TextInputLayout 或使用其他库。
我更喜欢选项 3。但是,我现在不知所措,既没有在文档中也没有通过谷歌搜索论坛找到任何解决方案或提示。
已解决
找到解决方案
阅读更多内容后,我发现已经在 Whosebug here 上提出了关于计数器 运行 的问题,并提供了解决方案建议。 但是,该解决方案并不像预期的那样使用简单的 xml 条目,而是必须以编程方式完成
我根据从 here 和 此处 .
收集的信息发布带有源代码的解决方案你所要做的就是像这样使用alignParent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.design="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/action_global_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginTop="8dp"
android:hint="@string/edit_comments"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:maxLength="@integer/global_comments_size"
android:layout_alignParentTop="true"
android:layout_above="@+id/textContainer"
android:scrollbars="vertical" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:overScrollMode="never"
android.support.design:errorEnabled="true"
android.support.design:hintEnabled="true"
app:counterEnabled="true"
app:counterMaxLength="@integer/global_comments_size"/></RelativeLayout>
我通过阅读文档和 Whosebug 找到了解决方案。 我在这里发布了一个完整的示例,以便更容易地重现该行为。
不幸的是,似乎没有一种方法可以简单地配置一个 xml 属性 让计数器停留在屏幕的可见区域。
解决方案在于使用接口 class TextWatchers,幸运的是它只需要几行代码。
1)创建res/values/strings.xml文件来定义最大字符大小
<integer name="max_comment_size">50</integer>
2) 在你的 activity 中添加以下两个视图 即res/layout/activity_main.xml
我喜欢把柜台放在最上面。如果您更喜欢将 TextView 和 TextEdit 放在底部,则可能需要交换它。
<TextView
android:id="@+id/constraint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remaining characters/Maximum characters: --/--" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:maxLength="@integer/max_comment_size" />
请注意,textMultiLine 不限制 EditText 字段的最大行数。它只限制布局大小。 使用 maxLength,您可以指定允许的最大字符数。
3)在java文件中使用TextWatcher统计输入的字符数并显示在TextView中 Java 文件在我的示例中是:java/com/example/mycompany/textcounter/MainActivity.java
接口class TextWatcher 有三个方法需要实现。但是,出于此目的,我们只对 onTextChange() 感兴趣。
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private EditText mEditText;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } //nothing to be done here
public void onTextChanged(CharSequence s, int start, int before, int count) {
Integer max_comment = getResources().getInteger(R.integer.max_comment_size);
mTextView.setText("Remaining characters" + "/Maximum characters: "+ String.valueOf(max_comment-s.length()) +"/" + max_comment);
}
public void afterTextChanged(Editable s) { } //nothing to be done here
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views
mEditText = (EditText) findViewById(R.id.editText);
mTextView = (TextView) findViewById(R.id.constraint_textview);
mEditText.addTextChangedListener(mTextEditorWatcher);
}
}
进一步说明:
解决方案基于提供的信息 here 关于计数器 运行 关闭屏幕和 here 关于如何使用 TextWatcher
也请参阅 android 关于 TextWatcher and TextEdit 的文档。
我用 maxLength 限制了 TextEdit 字段中输入字符的最大数量。 如果您确实需要限制最大行数,请参阅讨论和可能的解决方案 here and here 或使用 Whosebug 上的搜索。