편집 텍스트가 언제 편집되었는지 알기
편집 텍스트가 언제 편집되었는지 어떻게 알 수 있습니까? 사용자가 다음 상자를 선택하거나 소프트 키보드에서 완료 버튼을 누를 때와 같습니다.
입력을 클램핑 할 수 있도록 이것을 알고 싶습니다. 각 문자가 입력 된 후 텍스트 감시자의 afterTextChanged가 발생하는 것처럼 보입니다. 입력으로 몇 가지 계산을해야하므로 각 문자를 입력 한 후에는 계산을 피하고 싶습니다.
감사
이와 같은 것을 사용하여
meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_PREVIOUS:
yourcalc();
return true;
}
return false;
}
});
EditText
setOnFocusChangeListener
의 구현을 받는 상속합니다 OnFocusChangeListener
.
구현 onFocusChange
하고 .NET에 대한 부울 매개 변수가 hasFocus
있습니다. 이것이 거짓이면 다른 컨트롤에 대한 포커스를 잃은 것입니다.
편집하다
두 경우를 모두 처리하려면-포커스를 잃은 텍스트를 편집하거나 사용자가 "완료"버튼을 클릭합니다. 두 리스너에서 호출되는 단일 메서드를 만듭니다.
private void calculate() { ... }
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
calculate();
}
});
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
calculate();
}
});
다음과 같이 정의 된 EditText 객체 xml 사용 :
<EditText
android:id="@+id/create_survey_newquestion_editText_minvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="4"
android:imeOptions="actionDone"
android:inputType="number" />
i) 사용자가 소프트 키보드 (OnEditorActionListener)에서 완료 버튼을 클릭 할 때 또는 ii) EditText가 현재 다른 EditText에있는 사용자 포커스 (OnFocusChangeListener)를 잃었을 때 텍스트를 캡처 할 수 있습니다.
/**
* 3. Set the min value EditText listener
*/
editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue);
editText.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
String input;
if(actionId == EditorInfo.IME_ACTION_DONE)
{
input= v.getText().toString();
MyActivity.calculate(input);
return true; // consume.
}
return false; // pass on to other listeners.
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
String input;
EditText editText;
if(!hasFocus)
{
editText= (EditText) v;
input= editText.getText().toString();
MyActivity.calculate(input);
}
}
});
이것은 나를 위해 작동합니다. 다음과 같은 코드를 사용하여 계산을 한 후 소프트 키보드를 숨길 수 있습니다.
private void hideKeyboard(EditText editText)
{
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
편집 : onEditorAction에 반환 값 추가
포커스가 텍스트 편집에서 이동되면 텍스트를 가져 오면이 간단한 작업을 수행했습니다. 사용자가 버튼이나 다른 EditText 또는보기와 같은 다른보기를 선택하기 위해 포커스를 이동하면 작동합니다.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText editText = (EditText) v;
String text = editText.getText().toString();
}
}
});
더 높은 수준으로하려면 TextWatcher의 afterTextChanged () 메서드를 사용할 수 있습니다 .
참고 URL : https://stackoverflow.com/questions/5099814/knowing-when-edit-text-is-done-being-edited
'program story' 카테고리의 다른 글
IE에서 CSS 스타일 시트를 동적으로로드 할 수 없습니다. (0) | 2020.10.26 |
---|---|
현재 실행중인 DLL의 위치를 얻는 방법은 무엇입니까? (0) | 2020.10.26 |
CSS 3을 사용하여 세로로 정렬 (0) | 2020.10.26 |
Select2 Ajax 방법이 선택되지 않음 (0) | 2020.10.26 |
VBA의 생성자에 인수 전달 (0) | 2020.10.26 |