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

时间:2012-07-03 11:09:16

标签: 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_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>

2 个答案:

答案 0 :(得分:1)

使用Textwatcher。请检查以下链接:

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

onTextChanged()方法可用于监控邮件中剩余的字符数。

答案 1 :(得分:1)

检查源代码,......

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

            }
        });

谢谢..