0

在我的应用程序中,我想计算edittext中的宪章数量..,在字符数达到限制后,计数器应该再次以140开头,我想以“140/1”的格式显示它,在字符限制达到 0 之后,它再次统计为 140,但以 1 递增 1,并显示为“140/2”。我该如何做任何建议..??

edMessage.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {

                // Display Remaining Character with respective color
                 count = MAX_COUNT - s.length();
               Word_counter.setText(Integer.toString(count) + "/" + Integer.toString(word)  );

               Word_counter.setTextColor(Color.BLACK);
                if(count < 10)
                {
                     Word_counter.setTextColor(Color.YELLOW);
                }


            }
        });
4

1 回答 1

0

试试这个方法,希望这能帮助你解决你的问题。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private EditText editText;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textView);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                if(s.length()/140 == 0){
                    textView.setText("140");
                }else{
                    textView.setText("140/"+String.valueOf((s.length()/140)+1));
                }
            }
        });

    }
}
于 2014-06-30T05:58:48.090 回答