Android 如何像信息屏幕一样显示计数?
您好,我想在“发送”按钮下方键入消息时显示消息的剩余字符数,就像在消息屏幕中一样。我使用以下xml代码。如果有人知道,请帮助我:Android 如何像信息屏幕一样显示计数?,android,Android,您好,我想在“发送”按钮下方键入消息时显示消息的剩余字符数,就像在消息屏幕中一样。我使用以下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_hei
<?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"
android:background="#ffffff"
android:orientation="vertical" >
<EditText
android:id="@+id/txtTO"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:hint="To" />
<requestFocus />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@android:color/darker_gray"
android:padding="5dp" >
<EditText
android:id="@+id/txtMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Type the message" >
</EditText>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<TextView
android:id="@+id/tvCount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
使用
Textwatcher
。检查以下链接:
onTextChanged()
方法可用于监视消息中剩余的字符数。检查源代码
emailBody = (EditText) findViewById(R.id.editTxtBody);
emailBody.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
count = 256 - emailBody.length();
textCount.setText(Integer.toString(count));
textCount.setTextColor(Color.GREEN);
if (count < 10)
textCount.setTextColor(Color.YELLOW);
if (count < 0) {
InputFilter.LengthFilter lengthFilter;
lengthFilter = new InputFilter.LengthFilter(emailBody);
etMessage.setFilters(new InputFilter[] { lengthFilter });
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
emailBody=(EditText)findViewById(R.id.editTxtBody);
emailBody.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
count=256-emailBody.length();
setText(Integer.toString(count));
textCount.setTextColor(Color.GREEN);
如果(计数<10)
textCount.setTextColor(Color.YELLOW);
如果(计数<0){
InputFilter.LengthFilter LengthFilter;
lengthFilter=新的InputFilter.lengthFilter(emailBody);
setFilters(新的InputFilter[]{lengthFilter});
}
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
公共无效后文本已更改(可编辑){
}
});
谢谢