问题
I'm developping an android app , and i have an EditText
and a two RadioButtons
(A and B ),
what i'm trying to do is :
When RadioButton
A is checked , i want to change the keyboard layout to display it with the Done button ,
When the RadioButton
B is checked, i want to change the keyboard layout to display it with the Search Button .
I've tried to change the IMEOptions
of my EditText
like this , but it still doesn't work :
NB : the keyboard is already visible, what i want to do is just modify the button Search with the button Done in each case of the 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 about how to do that ??
Thanks in advance.
回答1:
What Android version are you targeting? I'm unable to post a comment sorry (new account), but am currently doing up a testcase to answer your question.
EDIT: Ok, I've figured it out. After a bit of googling (see this issue) and coding, I found that the imeOptions appear to be cached/tied to the input method. I'm not sure if this is a bug or intentional functionality. To switch the keyboard when the user taps either radio button, first make sure the inputType is set for your EditText (android:inputType="text"
), then use the following in your 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 out and re-setting of the InputType.
Finally, please be aware that many popular keyboard implementations don't give a damn what you set the imeOptions
to, so don't rely on this functionality in your app. Swype for example;
In conclusion (see what I did there?), not to be outdone, I've bundled up my test program. You can find it here: http://dl.dropbox.com/u/52276321/ChangeKeyboardTest.zip
回答2:
try use class handler to update views dynamically.
from: Change Button Text on Android
回答3:
try this RadioGroup
. It will work definitely. OR use 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
}
});
回答4:
Based on standing on the shoulders of giants, here's what I came up with. Thanks to @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 the lastIsDoneOk
shenanigans I went into an infinite loop. YMMV because I'm calling this from within a TextWatcher
, so you may not need it. Without keeping track of selectionStart
, Android brings the cursor back to the start.
来源:https://stackoverflow.com/questions/9480105/change-the-keyboard-layout-after-a-button-click