Android软键盘没有显示

时间:2015-12-09 15:16:33

标签: java android keyboard

我已按照本教程添加了自定义键盘:http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

但我不知道如何显示键盘?我见过几种方法,但在我的案例中没有一种方法可行。我试过的最后一个是:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput( 0, 0 );

这是android清单中的服务声明:

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

这是键盘布局,它与活动中加载的布局不同:

<?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" />

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>

我的基于IME的课程:

public class SimpleIME extends InputMethodService implements OnKeyboardActionListener
{
    private KeyboardView kv;
    private Keyboard keyboard;

    private boolean caps = false;

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

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

    @Override
    public void onPress( int primaryCode )
    {
    }

    @Override
    public void onRelease( int primaryCode )
    {
    }

    @Override
    public void onText( CharSequence text )
    {
    }

    @Override
    public void swipeDown()
    {
    }

    @Override
    public void swipeLeft()
    {
    }

    @Override
    public void swipeRight()
    {
    }

    @Override
    public void swipeUp()
    {
    }
}

我错过了什么吗?我怎么能显示这个键盘而不是android的默认键盘? (也没有显示)

谢谢。

编辑:我在settings / language et input找到了一个没有标签的复选框,但这与我的应用程序的键盘有关。我检查了它是否允许使用键盘,但它仍然没有出现。

编辑2:我还在settings / language and input / default中选择了我的键盘,但结果相同。

编辑3:当使用其他应用程序时键盘实际显示...: - /

0 个答案:

没有答案