简体   繁体   English

Android:字符在EditText中倒计时

[英]Android: Characters left countdown in EditText

I have this EditText with a limit of 150 characters: 我有这个EditText,限制为150个字符:

 <EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:layout_marginBottom="50dp"
    android:padding="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:maxLength="150"
    android:gravity="top|left"
    android:lineSpacingMultiplier="1.8"
    android:inputType="textMultiLine|textCapSentences" >
</EditText>

TextView: TextView的:

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

NewActivity: NewActivity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class NewActivity extends Activity {




private TextView textView1;
private EditText editText1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_text);


    final ImageButton a = (ImageButton) findViewById(R.id.imageBack);
    a.setOnClickListener(new OnClickListener()  {

        @Override
        public void onClick(View arg0) {
            Animation anim =              AnimationUtils.loadAnimation(NewActivity.this, R.anim.animation);
            a.startAnimation(anim);

            Intent intent = new Intent(NewActivity.this,MainActivity.class);
            startActivity(intent);

            editText1.addTextChangedListener(new TextWatcher()
            {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count)
                {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int aft)                                                                           
                {
                }

                @Override
                public void afterTextChanged(Editable s)
                {
                   // this will show characters remaining
                    textView1.setText(150 - s.toString().length() + "/150");
                }
            });


        }
    });




}}

I want to create a small box in the top right corner of the page where you can see how many characters there is left, for example 132/150, i have tried looking at others solutions here on stackoverflow but i couldnt get them to work. 我想在页面的右上角创建一个小盒子,你可以看到剩下多少个字符,例如132/150,我试着在stackoverflow上查看其他解决方案,但我无法让它们工作。

You can do this 你可以这样做

mEditText.addTextChangedListener(new TextWatcher()
{
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int aft                                                                           
    {
    }

    @Override
    public void afterTextChanged(Editable s)
    {
       // this will show characters remaining
        countTextView.setText(150 - s.toString().length() + "/150");
    }
});

You can use a TextWatcher to see when the text has changed 您可以使用TextWatcher查看文本何时更改

private TextView mTextView;
private EditText mEditText;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
           //This sets a textview to the current length
           mTextView.setText(String.valueOf(s.length()));
        }

        public void afterTextChanged(Editable s) {
        }
};

You can set the TextWatcher for the edittext with 您可以使用设置edittext的TextWatcher

mEditText.addTextChangedListener(mTextEditorWatcher);

Programatically do it like.. 以编程方式执行它...

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            int length = s.length();
            // set this length/maxlength to the textview at the top left
            // corner
            textView.setText(""+length+ "/" +max);//which is placed at the top right corner
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
  1. Keep a TextWatcher and track the length of entered text. 保留TextWatcher并跟踪输入文本的长度。
  2. Do MAX_LENGTH - yourTextwatcherString.length; 做MAX_LENGTH - yourTextwatcherString.length;
  3. Keep updating the view on the RHS top based on a timer/ or on even key press, your wish (there are several ways to do that). 根据计时器/或甚至按键,按照您的愿望继续更新RHS顶部的视图(有几种方法可以做到这一点)。 Check, whether old length and new length are not the same before updating the view. 在更新视图之前,检查旧长度和新长度是否相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM