programing

변경된 청취자 편집 텍스트에서 문자 수 계산

css3 2023. 9. 27. 18:04

변경된 청취자 편집 텍스트에서 문자 수 계산

내 프로젝트에서 나는.EditText. 의 캐릭터 수를 세고 싶습니다.EditText, 그리고 그 숫자를 a안에 보여줍니다.TextView. 제가 아래 코드를 작성해드렸고 잘 작동합니다.하지만, 제 문제는 클릭하면 숫자가 세지만 숫자를 줄여야 한다는 것입니다.어떻게 생각해야 합니까?

tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        i++;
        tv.setText(String.valueOf(i) + " / " + String.valueOf(charCounts));
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

사용하다

s.length()

한 번은 다음과 같은 답변이 제시되었지만 매우 비효율적입니다.

textMessage.getText().toString().length()

그냥 EditText에 있는 문자의 길이를 가져와서 표시하는 것은 어떻습니까?

일맥상통하는 것

tv.setText(s.length() + " / " + String.valueOf(charCounts));

당신의 코드에 약간의 변화가 있습니다.

TextView tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        tv.setText(String.valueOf(s.toString().length()));
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

이것은 미래의 시청자들을 위해 좀 더 많은 설명과 함께 약간 더 일반적인 대답입니다.

변경된 수신기 텍스트 추가

텍스트 길이를 찾거나 텍스트가 변경된 후 다른 작업을 수행하려면 변경된 수신기를 편집 텍스트에 추가할 수 있습니다.

EditText editText = (EditText) findViewById(R.id.testEditText);
editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});

수신기에는 다음과 같은 세 가지 방법이 필요합니다.beforeTextChanged,onTextChanged,그리고.afterTextChanged.

문자 수 세기

캐릭터 수를 입력할 수 있습니다.onTextChanged아니면beforeTextChanged와 함께

charSequence.length()

아니면afterTextChanged와 함께

editable.length()

메소드의 의미

매개변수가 조금 헷갈리므로 여기에 약간의 추가 설명이 있습니다.

텍스트 변경 전

beforeTextChanged(CharSequence charSequence, int start, int count, int after)

  • charSequence: 보류 중인 변경 전 텍스트 내용입니다.당신은 그것을 바꾸려고 하면 안됩니다.
  • start: 새 텍스트를 삽입할 위치의 인덱스입니다.범위를 선택하면 범위의 시작 인덱스가 됩니다.
  • count: 교체할 선택한 텍스트의 길이입니다.아무것도 선택하지 않은 경우count될 것이다0.
  • after: 삽입할 텍스트의 길이입니다.

텍스트가 변경된 경우

onTextChanged(CharSequence charSequence, int start, int before, int count)

  • charSequence: 변경 후 텍스트 내용입니다.여기서 이 값을 수정하려고 하면 안 됩니다.수정합니다.editable인에afterTextChanged필요하시면
  • start: 새 텍스트가 삽입된 시작 부분의 인덱스입니다.
  • before: 이것이 예전의 가치입니다.대체된 이전에 선택한 텍스트의 길이입니다.이 값은 다음과 같습니다.count인에beforeTextChanged.
  • count: 삽입된 텍스트 길이입니다.이 값은 다음과 같습니다.after인에beforeTextChanged.

텍스트 변경 후

afterTextChanged(Editable editable)

맘에 들다onTextChanged, 이는 이미 변경된 후에 호출됩니다.그러나 이제 텍스트가 수정될 수 있습니다.

  • editable: 편집 가능한 텍스트입니다.EditText. 하지만 변경할 경우 무한 루프에 빠지지 않도록 주의해야 합니다.자세한 내용은 설명서를 참조하십시오.

이 답변의 보충 이미지

enter image description here

TextWatcher 부부 상태TextWatcher = new TextWatcher() { @TextChanged 이전의 공용 공백 재정의(CharSequence charSequence, inti, inti1, inti2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        try {
            if (charSequence.length()==0){
                topMaritalStatus.setVisibility(View.GONE);
            }else{
                topMaritalStatus.setVisibility(View.VISIBLE);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

언급URL : https://stackoverflow.com/questions/4310525/counting-chars-in-edittext-changed-listener