我想知道,如果textarea视图有一个现成的植入,可以扩展为用户输入更多文本。喜欢android内置的短信息文本区域。
答案 0 :(得分:0)
答案 1 :(得分:0)
我得到了一个部分答案:) - 我也需要一个短信风格文本字段,所以我自己创建了一个,包括一个跟踪剩余字符数量的textwatcher。
这就是它的样子:
Textwatcher实施
public class SMSTextWatcher implements TextWatcher {
private final int maxLength;
private final int stringId;
private final TextView textView;
/**
* @param maxLength maximum number of characters that are allowed
* @param stringId must have at least one %s in it, for the updated character count.
* @param targetView target text view that displays the number of characters left.
*/
public SMSTextWatcher(int maxLength, int stringId, TextView targetView) {
this.maxLength = maxLength;
this.textView = targetView;
this.stringId = stringId;
updateTextView(maxLength);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
//Update textview (the limit is handled by the maxLength attribute)
updateTextView(maxLength - editable.length());
}
public void updateTextView(int charactersLeft) {
textView.setText(String.format(textView.getContext().getString(stringId), charactersLeft));
}
}
布局XML - 我们称之为widget_sms_field.xml:
<?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:orientation="vertical">
<EditText
android:id="@+id/sms_field_text"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:hint="@string/short_text"
android:imeOptions="actionSend|flagNoEnterAction"
android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
android:maxLength="@integer/sms_text_max_length"
android:paddingRight="10dp"
android:singleLine="false" />
<TextView
android:id="@+id/sms_field_remaining_characters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/remaining_characters"
android:textColor="@color/secondary_text" />
</LinearLayout>
最大长度
在上面的布局文件中我们使用了android:maxLength =“@ integer / sms_text_max_length”
要解决它,我们必须在项目的values文件夹中创建一个名为“integer.xml”的文件。文件内容应如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="sms_text_max_length" type="integer">160</item>
</resources>
如果您将文件命名为“整数”以外的其他内容 - 请务必同时更改@ integer / sms_text ....
“x字符左侧”文字
带有id sms_field_remaining_characters的小灰色文本字段(@ color / secondary_text - 您需要自己创建此颜色!)是将显示剩余字符数的字段。为此,请创建一个新的字符串资源:
<string name="remaining_characters">%s remaining Characters</string>
将小部件布局xml包含在您自己的布局中,并在Fragment的onViewCreated方法链接中将SMSTextWatcher链接到我们的短信风格字段:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText textField = (EditText) view.findViewById(R.id.sms_field_text);
textField.addTextChangedListener(new SMSTextWatcher(
getResources().getInteger(R.integer.sms_text_max_length),
R.string.remaining_characters,
(TextView) view.findViewById(R.id.sms_field_remaining_characters)));
}
完成:)!如果您有任何问题或建议,请与我们联系!