点击按钮后更改键盘布局

6
我正在开发一个安卓应用程序,我有一个EditText和两个RadioButtonsAB)。
我的目标是:
当选中RadioButton A时,我想更改键盘布局以显示带有“完成”按钮的布局, 当选中RadioButton B时,我想更改键盘布局以显示带有“搜索”按钮的布局。
我尝试像这样更改EditTextIMEOptions,但仍然不起作用:
NB: 键盘已经可见,我想做的只是在两个radioButtons的每种情况下修改按钮Search为按钮Done
@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();
    }   
}

有关如何做到这一点的任何想法吗?提前感谢。
4个回答

11
请问您的目标Android版本是什么?很抱歉我无法发表评论(因为是新账户),但我正在编写一个测试用例来回答你的问题。
编辑:好的,我已经找到了解决方法。经过一番谷歌搜索(请参阅这个问题)和编码,我发现imeOptions似乎被缓存/绑定到输入法中。我不确定这是否是一个错误或有意的功能。要在用户点击单选按钮时切换键盘,请首先确保EditText的inputType已设置(android:inputType="text"),然后在您的onCreate方法中使用以下代码:
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);
    }
});

请注意将InputType清零并重新设置的操作。

最后,请注意许多流行的键盘实现并不关心你设置了什么imeOptions,因此不要依赖此功能来开发你的应用程序。例如Swype键盘;

总之(你看我这样做了吗?),为了不被落后,我已经把我的测试程序打包起来了。你可以在这里找到它:http://dl.dropbox.com/u/52276321/ChangeKeyboardTest.zip

感谢aaronsnoswell的帮助,我现在会测试你的程序,并告诉你它是否适用于我 :) +1 - Houcine
1
小改进:int inputType = txtSearch.getInputType(); txtSearch.setInputType(InputType.TYPE_NULL); txtSearch.setImeOptions(EditorInfo.IME_ACTION_DONE); txtSearch.setInputType(inputType); - Harry
1
这几乎很棒。问题是它会将光标移回到行的开头。再次感谢,Android。 - JohnnyLambada
这对我有效,并且在将光标移回行的开头时没有遇到问题。 - Gary S.

1

基于借鉴前辈的经验,以下是我的想法。感谢 @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;
    }
}

没有使用lastIsDoneOk这个技巧,我就进入了无限循环。因为我是从中调用的,所以你可能不需要它。如果不跟踪selectionStart,Android会将光标带回开头。

0

尝试使用RadioGroup,它一定会起作用。或者使用链接。

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
    }
});

没有这样的方法setCloseImeOnDone(true); 你使用了哪个版本的Android SDK?我正在使用API级别10。 - Houcine
最佳示例在这里,请至少查看此链接...https://gist.github.com/475320 - Riddhish.Chaudhari

0

我的代码正在运行在UI线程上,因此我不需要使用Handler来更新UI键盘。 - Houcine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接