Change keyboard layout after pressing a button

I am developing an Android application and I have an EditText and two RadioButtons ( A and B ),

what i am trying to do:

When RadioButton A is checked, I want to change the keyboard layout to display it using the Finish button. When RadioButton B is checked, I want to change the keyboard layout to display it using the search button.

I tried changing the IMEOptions my EditText this way, but it still doesn't work:

NB: the keyboard is already visible, what I want to do, just change the Search button using the Finish button in each case of two RadioButtons

 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(btnA.isChecked() ) { txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE); // txtSearch.invalidate(); } else { txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH); // txtSearch.invalidate(); } } 

any ideas on how to do this?

Thanks in advance.

+4
source share
4 answers

What version of Android are you planning? I can't post a comment sorry (new account), but I'm currently doing a test test to answer your question.

EDIT: Alright, I figured it out. After a few search queries (see this question ) and coding, I found that imeOptions seem to be cached / bound to the input method. I'm not sure if this is a bug or intentional functionality. To switch the keyboard when the user clicked on one of the switches, first make sure inputType is set for your EditText ( android:inputType="text" ), and then use the following onCreate method:

 final RadioGroup btn_group = (RadioGroup) findViewById(R.id.btn_group); final RadioButton btnA = (RadioButton) findViewById(R.id.btnA); final RadioButton btnB = (RadioButton) findViewById(R.id.btnB); btn_group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { final EditText txtSearch = (EditText) findViewById(R.id.edit_text); txtSearch.setInputType(InputType.TYPE_NULL); if(btnA.isChecked()) { txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE); } else { txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH); } txtSearch.setInputType(InputType.TYPE_CLASS_TEXT); } }); 

Note the nulling and reconfiguration of InputType.

O0D3e.pngDdoxs.png

Finally, keep in mind that many popular keyboard implementations don't give a damn about the fact that you installed imeOptions , so don't rely on this functionality in your application. For example, Swype;

b4XeN.png

In conclusion (see what I did there?), In order to keep up, I linked my test program. You can find it here: http://dl.dropbox.com/u/52276321/ChangeKeyboardTest.zip

+9
source

try RadioGroup . This will work definitely. OR use the link

https://gist.github.com/475320

 // This will get the radiogroup RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1); // This will get the radiobutton in the radiogroup that is checked RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId()); // This overrides the radiogroup onCheckListener rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup rGroup, int checkedId) { switch(checkedId){ case R.id.btnA: txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE); break; case R.id.btnB: txtSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH); break;enter code here default: break; } txtSearch.setCloseImeOnDone(true); // close the IME when done is pressed } }); 
0
source

try using a class handler to dynamically update views.

from: Change button text on Android

0
source

Based on the position on the shoulders of the giants, this is what I came up with. Thanks @aaronsnoswell.

 private void imeSetup(boolean isDoneOk){ if (isDoneOk!=lastIsDoneOk) { int inputType = editText.getInputType(); int selectionStart = editText.getSelectionStart(); editText.setInputType(InputType.TYPE_NULL); if (isDoneOk) editText.setImeOptions(EditorInfo.IME_ACTION_DONE); else editText.setImeOptions(EditorInfo.IME_ACTION_NEXT); editText.setInputType(inputType); editText.setSelection(selectionStart); lastIsDoneOk = isDoneOk; } } 

Without lastIsDoneOk shenanigans, I went into an endless loop. YMMV, because I call it from within the TextWatcher , so you might not need it. Without tracking selectionStart , Android will return the cursor to the beginning.

0
source

Source: https://habr.com/ru/post/1398787/


All Articles