EditText 변경된 리스너에서 문자 계산
내 프로젝트에는 EditText
. 의 문자를 세고 EditText
그 숫자를에 표시 하고 싶습니다 TextView
. 다음 코드를 작성했으며 정상적으로 작동합니다. 그러나 내 문제는 클릭 Backspace할 때 카운트 업되지만 숫자를 줄여야합니다. 어떻게 고려할 수 Backspace있습니까?
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에서 char의 길이를 가져 와서 표시하는 것은 어떻습니까?
라인을 따라 뭔가
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) {
}
});
리스너에는 TextWatcher
,이 필요 beforeTextChanged
하며 onTextChanged
,, 및 세 가지 메소드를 재정의해야합니다 afterTextChanged
.
문자 수
당신의 글자 수를 얻을 수 있습니다 onTextChanged
또는 beforeTextChanged
함께
charSequence.length()
또는 afterTextChanged
와
editable.length()
방법의 의미
매개 변수는 약간 혼동되므로 여기에 약간의 추가 설명이 있습니다.
beforeTextChanged
beforeTextChanged(CharSequence charSequence, int start, int count, int after)
charSequence
: 이것은 보류중인 변경이 이루어지기 전의 텍스트 내용입니다. 변경하지 마십시오.start
: 새 텍스트를 삽입 할 인덱스입니다. 범위가 선택되면 범위의 시작 색인입니다.count
: 대체 할 선택된 텍스트의 길이입니다. 아무것도 선택되어 있지 않은 경우count
가 될 것이다0
.after
: 삽입 할 텍스트의 길이입니다.
onTextChanged
onTextChanged(CharSequence charSequence, int start, int before, int count)
charSequence
: 변경 후의 텍스트 내용입니다. 여기에서이 값을 수정하지 마십시오. 수정editable
에afterTextChanged
당신이 필요합니다.start
: 새 텍스트가 삽입 된 위치의 색인입니다.before
: 이것은 오래된 값입니다. 대체 된 이전에 선택한 텍스트의 길이입니다. 이는에서와 동일한 값count
입니다beforeTextChanged
.count
: 삽입 된 텍스트 길이입니다. 이는에서와 동일한 값after
입니다beforeTextChanged
.
afterTextChanged
afterTextChanged(Editable editable)
처럼 onTextChanged
, 이것은 이미 변경된 후에 호출됩니다. 그러나 이제 텍스트가 수정 될 수 있습니다.
editable
:의 편집 가능한 텍스트입니다EditText
. 그러나 변경하면 무한 루프에 빠지지 않도록주의해야합니다. 자세한 내용은 설명서 를 참조하십시오.
이 답변의 보충 이미지
TextWatcher maritalStatusTextWatcher = new TextWatcher () {@ 공개 void beforeTextChanged (CharSequence charSequence, int i, int i1, int i2) 재정의 {
}
@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
'Programing' 카테고리의 다른 글
UIButton에서 텍스트의 오른쪽에 이미지를 어떻게 배치합니까? (0) | 2020.03.26 |
---|---|
Quasiquote에서 Shapeless를 사용하는 방법? (0) | 2020.03.26 |
숨김을 다른 컴퓨터로 내보내기 (0) | 2020.03.26 |
몽구스의“__v”필드는 무엇입니까 (0) | 2020.03.26 |
관계형 데이터베이스 디자인 패턴? (0) | 2020.03.26 |