如何创建一个输入法android?

问题描述:

我想创建一个通用键盘,如Emoji。我正在关注this tutorial如何创建一个输入法android?

我希望主软键盘按原样工作,或者我的意思是扩展它。只想在符号显示时单击更改mModeChangeKey上的布局和事件。

当模式改变时,我想用表情符号显示我的键盘。并将其添加为其他表情符号。

的Manifest.xml

<service android:name="com.example.keyboardtesting.MyInputMethod" 
     android:label="@string/app_name" 
     android:permission="android.permission.BIND_INPUT_METHOD"> 
      <intent-filter> 
        <action android:name="android.view.InputMethod" /> 
      </intent-filter> 

      <meta-data android:name="android.view.im" 
       android:resource="@xml/method" /> 
    </service> 

还添加权限..

第一个按钮点击

startActivityForResult(new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS), 0); 

第二个按钮点击

InputMethodManager inputmethodmanager = (InputMethodManager) getSystemService("input_method"); 

    if (inputmethodmanager != null) 
    { 
     inputmethodmanager.showInputMethodPicker(); 
    } 

因为这是由用户选择两者。我们不能以编程方式进行。我也通过拉丁文和softkeyboard。但我仍然感到困惑。

MyInputMethod

public class MyInputMethod extends InputMethodService 
{ 
    private Keyboard mKeyboard; 

    private KeyboardView mInputView; 

    @Override 
    public void onInitializeInterface() 
    { 
     mKeyboard = new Keyboard(this, R.xml.qwerty); 
    } 

    @Override 
    public View onCreateInputView() 
    { 
     mInputView = (KeyboardView) getLayoutInflater().inflate(
       R.layout.input_black, null); 

     mInputView.setKeyboard(mKeyboard); 

     return mInputView; 
    } 
} 

简单的词:

我只是想表明我的键盘的Android设备上。我将在稍后添加事件。

+0

你是什么意思的“改变模式”。当你点击你创建的两个按钮时你期望得到什么? – 2015-11-22 10:29:42

如果您想要确保您的键盘显示,您还需要添加其他文件。喜欢这个。

创建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> 

让你的键盘布局。布局/ keyboard.xml

<?xml version="1.0" encoding="UTF-8"?> 
<android.inputmethodservice.KeyboardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/keyboard" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:keyPreviewLayout ="@layout/preview" 
/> 

的keyPreviewLayout是短命的弹出每当按下键盘上的键时显示出来的布局。

布局/ preview.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:background="#ffff00" 
    android:textStyle="bold" 
    android:textSize="30sp" 
    >  
</TextView> 

接下来你需要做的qwerty.xml功能。你已经创建了服务类。但是你还没有实施OnKeyboardActionListener。确保你做到了。