【问题标题】:Want custom keyboard to ONLY be used for MY APP and restore previous when app loses focus希望自定义键盘仅用于我的应用程序并在应用程序失去焦点时恢复以前的键盘
【发布时间】:2015-07-20 15:08:02
【问题描述】:

我按照this link 中的精彩大纲制作了这个“自定义键盘”,而不是使用 ECLIPSE。我使用的是 Android Studio (AS) 1.1.0。

这是我设备的屏幕截图:

唯一的问题是该过程需要更改Language and Input 的设置并且还需要替换所有应用程序的键盘。我不想要那个。我只希望我的应用程序为 ITSELF 更改键盘,然后在我的应用程序离开屏幕后立即恢复到以前的键盘,否则我将成为用户的一个巨大痛苦。与其这样做,我还不如只添加按钮来执行我希望通过自定义键盘调用的按键。 (这并不可怕;用户只需拉下通知栏并选择选择输入法,但对于大多数用户来说仍然过于侵入。)

结构如下:

qwerty.xml 中,通过执行大量操作更改了键盘:

<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 中的代码在软键盘上获取的内容(省略了空覆盖):

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

我想知道是否有人已经完成了我需要的工作:为一个应用程序实现自定义键盘,并在失去屏幕焦点时恢复原始键盘。

【问题讨论】:

    标签: java android xml keyboard


    【解决方案1】:

    我只想让我的应用程序自行更改键盘

    这是通过输入法编辑器系统无法实现的。负责用户使用的输入法的是用户,而不是您。

    与其那样做,我还不如只添加按钮来执行我希望通过自定义键盘调用的按键。

    这是你唯一的选择。

    【讨论】:

    • 不可能创建自定义键盘视图并通过InputConnection 与焦点EditText 进行通信吗?
    • @Suragch:我不知道。总的来说,我不推荐这些。文本输入很复杂:语言、可访问性等。此外,许多用户希望他们现有的输入法编辑器具有某些功能:基于滑动的单词输入、建议等。我强烈建议您让用户使用用户选择的任何键盘,而不是通过在您的应用中强制输入一些替代文本来将您的意愿强加于他们。
    • 总的来说我同意你的看法。然而,在内蒙古有很多说蒙古语的人,但很少有人在手机上安装垂直脚本蒙古语键盘。 (大多数使用中文键盘。)这就是为什么我在我正在制作的应用程序中提供蒙古语键盘。
    【解决方案2】:

    我遇到了同样的问题。您在这里使用的是输入法类。请尝试将自定义键盘用作 KeyboardView 类。 下面是代码的activity_main.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp"
    tools:context="com.example.t_sadhan.myapplication.MainActivity">
    
    <EditText
        android:id="@+id/editText0"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:inputType="text" />
    
    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText0"
        android:layout_centerHorizontal="true"
        android:inputType="text" />
    
    <!-- NOTE No need to develop a system service for keyboard, there is a standard View for that (well, not completely standard, its in a funny package 'android.inputmethodservice'. -->
    <!-- NOTE The graphical layout does not know the package ('java.lang.NoClassDefFoundError: Could not initialize class android.inputmethodservice.KeyboardView') so the keyboard is not shown. -->
    <android.inputmethodservice.KeyboardView
        android:id="@+id/keyboardView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:visibility="gone"
        android:keyBackground="@color/background_color"
        android:keyTextColor="#37474F"
        android:keyTextSize="20sp"
        android:fontFamily="sans-serif"
        android:background="#ECEFF1"/>
    

    有关如何创建键盘视图类的详细说明,请参阅http://www.fampennings.nl/maarten/android/09keyboard/index.htm

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2015-07-19
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多