如何像消息屏幕一样显示计数



嗨,我想显示当我们在发送按钮下方键入消息时,消息剩余的追逐者数量,就像在消息屏幕中一样。我正在使用下面的 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"
    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>

检查源代码,...

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) {
            }
        });

谢谢。。

使用 Textwatcher .检查以下链接:

http://developer.android.com/reference/android/text/TextWatcher.html

onTextChanged() 方法可用于监视消息中剩余的字符数。

最新更新