2

嗨,我想在我们在发送按钮下方键入消息时显示消息的剩余字符数,就像在消息传递屏幕中一样。我正在使用下面的 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>
4

2 回答 2

1

使用Textwatcher. 检查以下链接:

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

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

于 2012-07-03T11:13:57.693 回答
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) {

            }
        });

谢谢..

于 2012-07-03T11:15:21.653 回答