简体   繁体   中英

Custom Android Keyboard: How to Create Secondary Keyboard

I've done quite a bit of googling, and I can't find anything pertaining to exactly what I'm trying to do.

I've followed this tutorial: http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615 and I have it working as I want it to for my custom (Russian) keyboard layout. However I also want to be able to switch quickly back and forth between my custom Russian layout, and an English layout.

Right now I have to go to the top of my screen and select a new keyboard every time (which takes ~5s or so, but I'm frequently switching back and forth). I'd like to make it 1 click to toggle between keyboards.

If someone can explain to me how to create a key that allows me to switch to a second layout that would be greatly appreciated.

I cant thank you enough Vivek Mishra. You are a genius good sir!

I created a second xml file (engQwerty.xml) to store my second layout. From there all it took was in my SimpleIME.java file, in the method "onCreateInputView()", I created a second keyboard using my new xml file, as well as created a private boolean variable (in my case isRus) to keep track of which layout I was in.

  engKeyboard = new Keyboard(this, R.xml.engQwerty);
  isRus = true;

I then added a key, with the keycode -51 (arbitrarily chosen) to both xml files. Then back in the SimpleIME.java file, under the onKey function, I added:

  case -51 :

  if(isRus) {
      kv.setKeyboard(engKeyboard);
      isRus = false;
  }
  else {
      kv.setKeyboard(rusKeyboard);
      isRus = true;
  }

  break;

ie check which layout I'm currently in and toggle between them accordingly.

Lastly, if you want the caps or shift key to update your keyboard accordingly, in the "onKey" function, under "case Keyboard.KEYCODE_SHIFT:" make sure you add your new keyboard to become "setShifted".

  engKeyboard.setShifted(caps);

All credit to Vivek Mishra. Hopefully this can help other newbie android programmers such as myself find a solution to a fairly simple task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM