[android] EditText의 라이브 문자 수

Android에서 편집 텍스트 상자의 라이브 문자 수를 계산하는 가장 좋은 방법이 무엇인지 궁금합니다. 나는 이것을 보고 있었지만 이해가되지 않는 것 같았다.

문제를 설명하기 위해 EditText가 있고 문자를 150 자로 제한하려고합니다. 입력 필터로이 작업을 수행 할 수 있지만 텍스트 상자 바로 아래에 사용자가 입력 한 문자 수를 표시하고 싶습니다 (거의 스택 오버플로가 지금하고있는 것처럼).

누군가가 예제 코드의 작은 스 니펫을 작성하거나 올바른 방향으로 나를 가리킬 수 있다면 정말 감사하겠습니다.



답변

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

편집 텍스트에 대한 TextWatcher를 설정합니다.

mEditText.addTextChangedListener(mTextEditorWatcher);


답변

SupportLibrary v23.1에 도입 된 EditText 용 TextInputLayout 래퍼를 사용하여 xml 자체에서 문자 계산을 수행 할 수 있습니다.

EditText를 TextInputLayout으로 감싸고 CounterEnabled를 true로 설정하고 counterMaxLength를 설정하십시오.

<android.support.design.widget.TextInputLayout
    android:id="@+id/textContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="20"
    >
    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text Hint"
        />
</android.support.design.widget.TextInputLayout>

당신은 같은 재료 효과를 얻을 것이다

counterOverflowTextAppearance , counterTextAppearance 를 사용 하여 카운터 스타일을 지정할 수 있습니다 .

편집하다

Android 문서에서.

TextInputEditText의 클래스는이 레이아웃의 자식으로 사용할 수 있도록 제공됩니다. TextInputEditText를 사용하면 TextInputLayout이 텍스트 입력의 시각적 측면을 더 잘 제어 할 수 있습니다. 사용 예는 다음과 같습니다.

     <android.support.design.widget.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="@string/form_username"/>

 </android.support.design.widget.TextInputLayout>

TextInputLayout TextInputEditText


답변

다음 TextInputLayout과 같이 라이브러리와 호환 할 수 있습니다 .

app:counterEnabled="true"
app:counterMaxLength="420"

완료 :

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="420">

    <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:maxLength="420" />

</android.support.design.widget.TextInputLayout>


답변

xml에서 editText에이 속성을 추가하십시오.

    android:maxLength="80"

자바 에서이 리스너 추가

  ed_caption.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) {
            tv_counter.setText(80 - s.toString().length() + "/80");

        }
    });


답변

매우 간단합니다. 아래 지침을 따르십시오.

==== 가져 오기에 추가 ===

import android.text.Editable;
import android.text.TextWatcher;

=====이 정의 =====

private TextView sms_count;

========== 만들기 내부 =====

sms_count = (TextView) findViewById(R.id.textView2);


final TextWatcher txwatcher = new TextWatcher() {
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   }

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

      sms_count.setText(String.valueOf(s.length()));
   }

   public void afterTextChanged(Editable s) {
   }
};

sms_message.addTextChangedListener(txwatcher);


답변

    You can use TextWatcher class to see text has changed and how much number of character remains.Here i have set counter of 140 characters.

    EditText typeMessageToPost;
    TextView number_of_character;
public void onCreate(Bundle savedBundleInstance) {
        super.onCreate(savedBundleInstance);
setContentView(R.layout.post_activity);
typeMessageToPost.addTextChangedListener(mTextEditorWatcher);
}
private final TextWatcher mTextEditorWatcher=new TextWatcher() {

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

        }

        @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
            number_of_character.setText(String.valueOf(140-s.length()));
        }
    };


답변

TextInputLayoutXML 파일에 다음 두 줄을 설정 하십시오.

app:counterEnabled="true"
app:counterMaxLength="200"