Android 任何可扩展文本区域(SMS消息)的就绪实现
我想知道是否有一个textarea视图可以随着用户输入更多文本而扩展。就像android内置的短信文本区 我得到了部分答案:)-我还需要一个短信风格的文本字段,所以我自己做了一个,包括一个跟踪剩余字符数的文本观察程序 这就是它的样子: Textwatcher实现Android 任何可扩展文本区域(SMS消息)的就绪实现,android,Android,我想知道是否有一个textarea视图可以随着用户输入更多文本而扩展。就像android内置的短信文本区 我得到了部分答案:)-我还需要一个短信风格的文本字段,所以我自己做了一个,包括一个跟踪剩余字符数的文本观察程序 这就是它的样子: Textwatcher实现 public class SMSTextWatcher implements TextWatcher { private final int maxLength; private final int stringId
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\u sms\u 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>
完成:)!如果您有问题或建议,请告诉我
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="sms_text_max_length" type="integer">160</item>
</resources>
<string name="remaining_characters">%s remaining Characters</string>
@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)));
}