自定义软键盘为Android运行时更改

时间:2015-03-10 08:13:37

标签: android keyboard android-softkeyboard android-input-method

我已经制作了一个软键盘应用程序,如下所示:custom soft keyboard

现在我需要一个从MainActivity更改键盘背景的选项。我该怎么做?
这是代码:

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

这是带有按钮的MainActivity,它应该改变背景:

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.test_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            //Change keyboard background
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

是否要在背景布局上添加图片或更改为主题?

要仅更改背景颜色,您需要在布局文件中添加背景颜色,对于主题,您需要创建主题,而不是在java代码上实现。喜欢主题作为样本;

//对于样本默认主题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:orientation="vertical"

    android:id="@+id/mainview">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorBack">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:id="@+id/menu_item"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_more_vert_white_24dp"/>
    </RelativeLayout>

    <android.inputmethodservice.KeyboardView
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:keyTextColor="@color/colorAccent"
        android:padding="4px"
        android:background="@drawable/images"
        android:keyTextSize="18sp"
        android:keyBackground="@color/colorTransparent"
        android:keyPreviewLayout ="@layout/preview"/>
    </LinearLayout>

对于新创建的默认主题实现,java将在下面:

    @Override
    public View onCreateInputView() {
    View view = null;
    if (themes != null && themes.equals("default")) {
            LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.default_theme, null);
            imageView = (ImageView) view.findViewById(R.id.menu_item);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openOptionsMenu(v);
                }
            });
            kv = (KeyboardView) view.findViewById(keyboard);
            //kv.setBackgroundResource(R.drawable.images);
            qwertyKeyboard = new Keyboard(this, R.xml.qwerty);
            kv.setKeyboard(qwertyKeyboard);
            kv.setOnKeyboardActionListener(this);
            kv.setOnTouchListener(new HapticListen(7));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                kv.setBackground(drawable);
            }
            return view;
        }else {
            return  null;
        }
      }

希望我会帮助你理解。