希望自定义键盘仅用于我的APP并在应用失去焦点时恢复以前的键盘 [英] Want custom keyboard to ONLY be used for MY APP and restore previous when app loses focus

查看:87
本文介绍了希望自定义键盘仅用于我的APP并在应用失去焦点时恢复以前的键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此链接并且不使用ECLIPSE.我使用的是Android Studio( AS )1.1.0.

I made this "custom keyboard" by following the wonderful outline in this link AND NOT USING ECLIPSE. I used Android Studio (AS) 1.1.0.

这是我设备上的屏幕截图:

Here's a screenshot from my device:

唯一的问题是该过程要求更改Language and Input的设置,并且还替换了FOR ALL APPS键盘.我不要我只希望我的应用程序更改ITSELF的键盘,然后在我的应用程序退出屏幕后立即恢复为以前的键盘,否则我将在用户屁股上感到极大的痛苦.除了这样做,我还可以添加按钮以执行希望通过自定义键盘调用的按键. (这并不可怕;用户只需要拉下通知栏并选择选择输入法即可,但对于大多数用户而言仍然太麻烦了.)

The only problem is that the procedure requires changing settings for Language and Input AND ALSO replaces the keyboard FOR ALL APPS. I don't want that. I just want MY app to change the keyboard for ITSELF and then revert to the previous keyboard as soon as my app goes off screen, otherwise I'm going to be a huge pain in users' butts. Rather than do that, I may as well just add buttons to perform the keypresses I hoped to invoke via a custom keyboard. (It's not gruesome; user just has to pull down the notification bar and select choose input method, but still too intrusive for most users.)

结构如下:

qwerty.xml中,通过执行以下许多操作来更改键盘:

The keypad is altered in qwerty.xml by doing lots of this:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
          android:keyWidth="10%p"
          android:horizontalGap="0px"
          android:verticalGap="0px"
          android:keyHeight="60dp"
    >
    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
    </Row>
...

这里是method.xml:

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
    <subtype
        android:label=              "@string/subtype_en_US"
        android:imeSubtypeLocale=   "en_US"
        android:imeSubtypeMode=     "keyboard" 
    />
</input-method>

这里是AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.dslomer64.simplekeyboard">

    <application

                android:allowBackup="true"
                android:label="@string/app_name"
                android:icon="@mipmap/ic_launcher"
                android:theme="@style/AppTheme">

                <service android:name=".SimpleIME"
                     android:label="@string/simple_ime"
                     android:permission="android.permission.BIND_INPUT_METHOD"
                >
                <meta-data android:name="android.view.im" android:resource="@xml/method"/>
                <intent-filter>
                    <action android:name="android.view.InputMethod" />
                </intent-filter>
        </service>

    </application>

</manifest>

添加到清单中的service是通过SimpleIME.java中的代码在软键盘上获得的内容(省略了空替代):

The service that's added to the manifest is what gets at the soft keyboard through the code in SimpleIME.java (empty overrides omitted):

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputConnection;

public class SimpleIME extends InputMethodService
    implements KeyboardView.OnKeyboardActionListener
{

  private KeyboardView kv;
  private Keyboard keyboard;

  private boolean caps = false;

  @Override
  public View onCreateInputView() {
    kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return kv;
  }
  private void playClick(int keyCode){
    AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
    switch(keyCode){
      case 32:
        am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
        break;
      case Keyboard.KEYCODE_DONE:
      case 10:
        am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
        break;
      case Keyboard.KEYCODE_DELETE:
        am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
        break;
      default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
    }
  }
  @Override
  public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();
    playClick(primaryCode);
    switch(primaryCode){
      case Keyboard.KEYCODE_DELETE :
        ic.deleteSurroundingText(1, 0);
        break;
      case Keyboard.KEYCODE_SHIFT:
        caps = !caps;
        keyboard.setShifted(caps);
        kv.invalidateAllKeys();
        break;
      case Keyboard.KEYCODE_DONE:
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
        break;
      default:
        char code = (char)primaryCode;
        if(Character.isLetter(code) && caps){
          code = Character.toUpperCase(code);
        }
        ic.commitText(String.valueOf(code),1);
    }
  }
    ...
}

我想知道是否有人完成了我需要做的事情:实施一个仅适用于一个应用程序的自定义键盘,并在失去屏幕焦点时恢复原始状态.

I wonder if anyone out there has done what I need: implement a custom keyboard JUST FOR ONE APP and restore original upon loss of screen focus.

推荐答案

我只希望我的应用为ITSELF更改键盘

I just want MY app to change the keyboard for ITSELF

通过输入法编辑器系统无法实现.用户(而不是您)负责用户使用的输入法.

That is not possible through the input method editor system. The user, not you, is in charge of the input method that the user uses.

除了这样做,我还可以添加按钮以执行希望通过自定义键盘调用的按键.

Rather than do that, I may as well just add buttons to perform the keypresses I hoped to invoke via a custom keyboard.

那是您唯一的选择.

这篇关于希望自定义键盘仅用于我的APP并在应用失去焦点时恢复以前的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆