如何在onSwipe方法中更改键盘布局?

3
我想知道如何在onSwipe方法中更改键盘布局。
我有包含字母的qwerty.xml,但我需要数字和符号。
这两组字符在numeric.xml中。
当我向左或向右滑动时,将显示numeric.xml并隐藏qwerty.xml
如果您需要我的代码的任何部分,请询问。
1个回答

2
你需要在InputManagerService中更改键盘对象的布局。然后,你需要使所有按键失效,以便重新绘制键盘。 就像这样:
 public class MyKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener
 {

    private KeyboardView myKeyboardView;
    private Keyboard myKeyboard;

    public View onCreateInputView()
    {

       viewOfKeyboard = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
       theKeyboardLayout = new Keyboard(this, R.xml.keyboardlayout);
       viewOfKeyboard.setKeyboard(theKeyboardLayout);
       viewOfKeyboard.setOnKeyboardActionListener(this);
       return myKeyboardView;
    }

    public void swipeRight()
    {
       if(isQwerty)
       {
         myKeyboard = new Keyboard(this, R.xml.numeric);
         //Creates a new keyboard object.
         myKeyboardView.setKeyboard(myKeyboard);
         //Assigns the new keyboard object to the keyboard view
         myKeyboardView.invalidateAllKeys();
         //Makes android redraw the keyboard view, with the new layout.
         isqwerty = false;
       }
       else
       {
         myKeyboard = new Keyboard(this, R.xml.qwerty); 
         myKeyboardView.setKeyboard(myKeyboard);
         myKeyboardView.invalidateAllKeys();
         isqwerty = true;
       }

       //.........the rest of MyKeyboard's methods     

    }

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