2016-12-26 63 views
1

我来自javascript背景,这是我第一次使用java。我一直在关注this guide。我有一个完全可用的键盘,现在我想开始定制它。我想要做的第一件事是按下按键时显示符号。如何调用setInputView方法渲染不同的键盘

经过挖掘它看起来像setInputView可能是我需要的,但我不知道如何调用它?有一个switch语句控制着我认为可以挂钩的符号的外壳,但是当我调用方法时,我得到错误Attempt to invoke interface method 'void android.inputmethodservice.KeyboardView$OnKeyboardActionListener.onPress(int)' on a null object reference,我认为这是因为我想打电话的功能,我不是实际上呼叫。

以下是相关的Java代码我到目前为止有:

包com.example.crhistian.spanishsymbolskeyboard;

import android.inputmethodservice.InputMethodService; 
import android.inputmethodservice.KeyboardView; 
import android.inputmethodservice.Keyboard; 
import android.view.View; 
import android.view.inputmethod.InputConnection; 
import android.view.KeyEvent; 
import android.media.AudioManager; 
public class SpanishSymbols extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener{ 

private KeyboardView kv; 
private Keyboard keyboard; 
private KeyboardView spanishkv; 

private boolean caps = false; 

@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_MODE_CHANGE : 
      setInputView(spanishkv); 
      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() { 
} 

@Override 
public View onCreateInputView() { 
    kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null); 
    keyboard = new Keyboard(this, R.xml.keyboard); 
    spanishkv = (KeyboardView)getLayoutInflater().inflate(R.layout.spanishsymbols, null); 
    spanishkv.setKeyboard(keyboard); 
    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); 
    } 
} 

}

+0

在这行越来越问题? –

+0

at this line'case Keyboard.KEYCODE_MODE_CHANGE: setInputView(spanishkv);'我知道它是因为我试图调用setInputView错误,但我不知道如何正确执行它 –

回答

1

您需要设置监听西班牙键盘上,像

spanishKv.setOnKeyboardActionListener(this);