소프트 키보드에서 "완료"키 누름을 포착하는 방법
소프트 키보드에서 특정 키 이벤트를 어떻게 포착합니까? 특히 "Done"키에 관심이 있습니다.
참고 : 이 답변은 오래되었으며 더 이상 작동하지 않습니다. 아래 답변을 참조하십시오.
KeyEvent를 포착 한 다음 해당 키 코드를 확인합니다. FLAG_EDITOR_ACTION은 Enter 키가 "next"또는 "done"으로 자동 레이블 지정된 IME에서 오는 Enter 키를 식별하는 데 사용됩니다.
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
//your code here
받아 들여진 답변에 어떤 종류의 청취자가 사용되었는지 잘 모르겠습니다. 나는에 OnKeyListener
첨부 된 것을 사용 EditText
했고 그것은 다음을 잡을 수 없었고 끝났다.
그러나 working을 사용 OnEditorActionListener
하면 액션 값을 정의 된 상수 EditorInfo.IME_ACTION_NEXT
및 EditorInfo.IME_ACTION_DONE
.
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
doSomething();
return true;
}
else {
return false;
}
}
});
@Swato의 대답은 나를 위해 완전하지 않았고 컴파일하지도 않았으므로 DONE 및 NEXT 작업과 비교하는 방법을 보여줍니다.
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
case EditorInfo.IME_ACTION_DONE:
// done stuff
break;
case EditorInfo.IME_ACTION_NEXT:
// next stuff
break;
}
}
});
또한 JellyBean 이상에서는 OnEditorActionListener가 'enter'또는 'next'를 수신하는 데 필요하며 OnKeyListener를 사용할 수 없다는 점을 지적하고 싶습니다. 문서에서 :
소프트 입력 방법은 텍스트를 입력하는 여러 가지 독창적 인 방법을 사용할 수 있으므로 소프트 키보드에서 키를 누르면 키 이벤트가 생성된다는 보장이 없습니다. 이는 IME의 재량에 맡겨져 있으며 실제로 이러한 이벤트를 보내는 것은 권장되지 않습니다 . 소프트 입력 방법의 키에 대해 KeyEvents 수신에 의존해서는 안됩니다.
참조 : http://developer.android.com/reference/android/view/KeyEvent.html
다음과 같이하십시오.
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//Do Something
}
return false;
}
});
etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
etSearchFriends.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
"완료"키를 잡으려면 소프트 키보드에서 활동의 onKeyUp 메서드를 재정의합니다. 뷰에 대한 OnKeyListener 리스너 설정은 작동하지 않습니다. 소프트웨어 입력 메소드에서 키를 누르면 일반적으로이 리스너의 메소드를 트리거하지 않기 때문에이 콜백은 뷰에서 하드웨어 키를 누를 때 호출됩니다.
// Called when a key was released and not handled by any of the views inside of the activity.
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// code here
break;
default:
return super.onKeyUp(keyCode, event);
}
return true;
}
I have EditText that searches names, and it automatically shows results below in ListView. SoftInput keyboard only showed "next" button and enter sign - which didn't do anything. I wanted only Done button (no next or enter sign) and also I wanted it when it was pressed, it should close keyboard because user should see results below it.
Solution that I found /by Mr Cyril Mottier on his blog/ was very simple and it worked without any additional code: in xml where EditText is located, this should be written: android:imeOptions="actionDone"
so hidding keyboard with Done button, EditText should look like this:
<EditText
android:id="@+id/editText1central"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_toLeftOf="@+id/imageView2tie"
android:ems="10"
android:imeOptions="actionDone"
android:hint="@string/trazi"
android:inputType="textPersonName" />
Note : inputtype mention in your edittext.
<EditText android:id="@+id/select_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
edittext.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
//do something here.
return true;
}
return false;
}
});
you can override done key event by this method:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// code here
}
return false;
}
});
IME_MASK_ACTION is 255, while the received actionId is 6, and my compiler does not accept
if (actionId & EditorInfo.IME_MASK_ACTION)
which is an int. What is the use of &-ing 255 anyway? So the test simply can be
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE)
...
참고URL : https://stackoverflow.com/questions/3031887/how-to-catch-a-done-key-press-from-the-soft-keyboard
'IT TIP' 카테고리의 다른 글
삼각파를 생성하는 단선 함수가 있습니까? (0) | 2020.11.29 |
---|---|
UISegmentedControl 레지스터는 선택한 세그먼트를 탭합니다. (0) | 2020.11.29 |
폴더 선택 대화 상자 WPF (0) | 2020.11.29 |
어셈블리 코드를 얻기 위해 Linux에서 바이너리 실행 파일을 분해하는 방법은 무엇입니까? (0) | 2020.11.29 |
클릭 후 입력 텍스트를 지우려면 어떻게합니까 (0) | 2020.11.29 |