使用Input方法连接视图

时间:2012-04-19 13:14:05

标签: android view android-softkeyboard custom-view

我通过扩展View类创建了一个自定义视图。点击后我想要弹出软键盘,就像使用edittext一样(即打开预测文本并能够选择所有可用的输入法)。

我尝试让视图扩展onclicklistener,在调用时,使用InputMethodManager显示软键盘,然后使用Onkeydown来拦截按键。这是笨重的,不起作用,因为:

  1. 我只能在标准文本和数字输入方法之间切换。没有其他输入方法可行(如果用户有一个,我需要能够切换到日语IME,就像编辑文本一样)。

  2. 没有预测文本,这对于该程序是绝对必要的,因为它需要用户输入日语汉字。

  3. 有没有办法在标准视图和IME之间建立类似于edittext的连接?

1 个答案:

答案 0 :(得分:1)

要显示预测文本,您应设置可由系统接受的EditorInfo类型。像这样:

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
       // TODO Auto-generated method stub
       // Set your EditorInfo type in the onCreateInputConnection
       outAttrs.actionLabel = null;
       outAttrs.inputType = InputType.TYPE_CLASS_TEXT;
       outAttrs.imeOptions = EditorInfo.IME_ACTION_NEXT;
       return new MyBaseInputConnection(this, false);
}