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 に答える