Android: 키보드 입력 버튼을 "검색"이라고 말하고 클릭을 처리하는 방법은 무엇입니까?
이해할 수가 없어요.일부 앱에는 텍스트 편집(텍스트 상자)이 있으며, 텍스트를 터치하면 화면 키보드가 나타나면 키보드에 입력 키 대신 "검색" 단추가 있습니다.
저는 이것을 구현하고 싶습니다.어떻게 하면 검색 버튼을 구현하고 검색 버튼의 누름을 감지할 수 있습니까?
편집: 검색 버튼을 구현하는 방법을 찾았습니다. XML에서,android:imeOptions="actionSearch"또는 자바어로,EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);그런데 그 검색 버튼을 누르는 사용자를 어떻게 처리해야 합니까?그것과 관련이 있습니까?android:imeActionId?
레이아웃에서 검색할 입력 방법 옵션을 설정합니다.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
Java에 편집자 작업 수신기를 추가합니다.
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;
}
});
아래의 코틀린 사용:
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch()
}
true
}
사용자가 검색을 클릭할 때 키보드를 숨깁니다.Robby Pond 답변에 추가
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(editText.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>
이 답변은 텍스트 입력 편집용입니다.텍스트:
레이아웃 XML 파일에서 입력 방법 옵션을 필요한 유형으로 설정합니다.예를 들어 완성된
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/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"/>
</com.google.material.text 필드입니다.텍스트 입력 레이아웃>
마찬가지로 timeOptions를 actionSubmit, actionSearch 등으로 설정할 수도 있습니다.
Java에 편집자 작업 수신기를 추가합니다.
TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);
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;
}
});
코틀린을 사용하는 경우:
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" />
Java 기준:
editText.clearFocus();
InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
언급URL : https://stackoverflow.com/questions/3205339/android-how-to-make-keyboard-enter-button-say-search-and-handle-its-click
'programing' 카테고리의 다른 글
| wpf 창에 사용자 컨트롤 추가 (0) | 2023.06.03 |
|---|---|
| UI 이미지를 가져와서 검은색 테두리를 지정하려면 어떻게 해야 합니까? (0) | 2023.06.03 |
| Ruby on Rails 문자열에서 html 제거 (0) | 2023.06.03 |
| Swift에서 NSDocument Directory를 찾는 방법은 무엇입니까? (0) | 2023.06.03 |
| Android 응용 프로그램에서 인터넷에 액세스하려면 어떤 권한이 필요합니까? (0) | 2023.06.03 |