반응형
안드로이드 : 키보드 입력 버튼이 "검색"이라고 말하고 클릭을 처리하는 방법은 무엇입니까?
나는 이것을 알아낼 수 없다. 일부 앱에는 EditText (텍스트 상자)가 있는데,이를 터치하면 화면 키보드가 표시되고 키보드에는 Enter 키 대신 "검색"버튼이 있습니다.
이것을 구현하고 싶습니다. 해당 검색 버튼을 구현하고 검색 버튼의 누름을 어떻게 감지합니까?
편집 : 검색 버튼을 구현하는 방법을 찾았습니다. XML에서, android:imeOptions="actionSearch"
또는 자바, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
. 그러나 사용자가 검색 버튼을 누르면 어떻게 처리합니까? 그것과 관련이 android:imeActionId
있습니까?
레이아웃에서 입력 방법 옵션을 검색하도록 설정하십시오.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
자바에서 에디터 액션 리스너를 추가하십시오.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
사용자가 검색을 클릭하면 키보드를 숨 깁니다. 로비 연못 답변에 추가
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
//...perform search
}
에 xml
파일을 넣어 imeOptions="actionSearch"
및 inputType="text"
, maxLines="1"
:
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1" />
코 틀린에서
evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doTheLoginWork()
}
true
}
부분 Xml 코드
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="@+id/evLoginUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:textColor="@color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="@+id/evLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:textColor="@color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
이 답변은 TextInputEditText입니다.
레이아웃 XML 파일에서 입력 방법 옵션을 필요한 유형으로 설정하십시오. 예를 들어 완료 .
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"/>
마찬가지로 imeOptions를 actionSubmit, actionSearch 등으로 설정할 수도 있습니다.
자바에서 에디터 액션 리스너를 추가하십시오.
textInputLayout.getEditText().setOnEditorActionListener(new
TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction();
return true;
}
return false;
}
});
kotlin을 사용하는 경우 :
textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction()
}
true
}
XML로 :
<EditText
android:id="@+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text" />
자바로 :
editText.clearFocus();
InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
반응형
'Programming' 카테고리의 다른 글
Eclipse 프로젝트에서 자바 스크립트 유효성 검사를 어떻게 제거합니까? (0) | 2020.02.29 |
---|---|
EC2 인스턴스에서 인스턴스 ID를 얻는 방법은 무엇입니까? (0) | 2020.02.29 |
대괄호 사이에서 텍스트를 추출하는 정규식 (0) | 2020.02.29 |
JavaScript에서 시간을 비교하지 않고 날짜 부분 만 비교 (0) | 2020.02.29 |
JavaScript에서 URL의 호스트 이름 부분을 추출하는 방법 (0) | 2020.02.29 |