ImeOptions의 완료 버튼 클릭을 어떻게 처리합니까?
EditText
사용자가 EditText를 클릭 할 때 키보드에 완료 버튼을 표시 할 수 있도록 다음 속성을 설정 하는 위치가 있습니다.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
사용자가 화면 키보드에서 완료 버튼을 클릭하면 (입력 완료) RadioButton
상태 를 변경하고 싶습니다 .
화면 키보드에서 완료된 버튼을 어떻게 추적합니까?
나는 Roberts와 chirags 답변의 조합으로 끝났습니다.
((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Identifier of the action. This will be either the identifier you supplied,
// or EditorInfo.IME_NULL if being called due to the enter key being pressed.
if (actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
onSearchAction(v);
return true;
}
// Return true if you have consumed the action, else false.
return false;
}
});
업데이트 : 위의 코드는 때로는 콜백을 두 번 활성화합니다. 대신 Google 채팅 클라이언트에서 얻은 다음 코드를 선택했습니다.
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// If triggered by an enter key, this is the event; otherwise, this is null.
if (event != null) {
// if shift key is down, then we want to insert the '\n' char in the TextView;
// otherwise, the default action is to send the message.
if (!event.isShiftPressed()) {
if (isPreparedForSending()) {
confirmSendMessageIfNeeded();
}
return true;
}
return false;
}
if (isPreparedForSending()) {
confirmSendMessageIfNeeded();
}
return true;
}
이것을 시도하십시오. 필요한 것에서 작동해야합니다.
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do here your stuff f
return true;
}
return false;
}
});
<EditText android:imeOptions="actionDone"
android:inputType="text"/>
그렇다면 자바 코드는
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Here you can write the code");
return true;
}
return false;
}
});
나는이 질문이 오래되었다는 것을 알고 있지만 나를 위해 무엇이 효과가 있었는지 지적하고 싶다.
I tried using the sample code from the Android Developers website (shown below), but it didn't work. So I checked the EditorInfo class, and I realized that the IME_ACTION_SEND integer value was specified as 0x00000004
.
Sample code from Android Developers:
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextEmail
.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
/* handle action here */
handled = true;
}
return handled;
}
});
So, I added the integer value to my res/values/integers.xml
file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="send">0x00000004</integer>
</resources>
Then, I edited my layout file res/layouts/activity_home.xml
as follows
<EditText android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeActionId="@integer/send"
android:imeActionLabel="@+string/send_label"
android:imeOptions="actionSend"
android:inputType="textEmailAddress"/>
And then, the sample code worked.
While most people have answered the question directly, I wanted to elaborate more on the concept behind it. First, I was drawn to the attention of IME when I created a default Login Activity. It generated some code for me which included the following:
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>
You should already be familiar with the inputType attribute. This just informs Android the type of text expected such as an email address, password or phone number. The full list of possible values can be found here.
It was, however, the attribute imeOptions="actionUnspecified"
that I didn't understand its purpose. Android allows you to interact with the keyboard that pops up from bottom of screen when text is selected using the InputMethodManager
. On the bottom corner of the keyboard, there is a button, typically it says "Next" or "Done", depending on the current text field. Android allows you to customize this using android:imeOptions
. You can specify a "Send" button or "Next" button. The full list can be found here.
With that, you can then listen for presses on the action button by defining a TextView.OnEditorActionListener
for the EditText
element. As in your example:
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do here your stuff f
return true;
}
return false;
}
});
Now in my example I had android:imeOptions="actionUnspecified"
attribute. This is useful when you want to try to login a user when they press the enter key. In your Activity, you can detect this tag and then attempt the login:
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
More details on how to set the OnKeyListener, and have it listen for the Done button.
First add OnKeyListener to the implements section of your class. Then add the function defined in the OnKeyListener interface:
/*
* Respond to soft keyboard events, look for the DONE press on the password field.
*/
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Done pressed! Do something here.
}
// Returning false allows other listeners to react to the press.
return false;
}
Given an EditText object:
EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);
Thanks to chikka.anddev and Alex Cohn in Kotlin it is:
text.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE ||
event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {
doSomething()
true
} else {
false
}
}
Here I check for Enter
key, because it returns EditorInfo.IME_NULL
instead of IME_ACTION_DONE
.
참고 URL : https://stackoverflow.com/questions/2004344/how-do-i-handle-imeoptions-done-button-click
'IT story' 카테고리의 다른 글
Laravel 4 : Eloquent ORM을 사용하여 "주문"하는 방법 (0) | 2020.05.25 |
---|---|
오류 : Postgres를 사용하여 city_id_seq 시퀀스에 대한 권한이 거부되었습니다. (0) | 2020.05.25 |
문자열의 일부를 다른 문자열로 교체 (0) | 2020.05.25 |
MySQL : GROUP_CONCAT 값 정렬 (0) | 2020.05.25 |
키 값 쌍을 가진 array_push () (0) | 2020.05.25 |