android - Android 运行时更改的自定义软键盘

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

最佳答案

你想在背景布局上添加图像还是更改为主题?

如果只更改背景颜色,您需要在布局文件中添加背景颜色,对于主题,您需要先创建主题,然后再在 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;
        }
      }

希望我能帮助您理解。

关于android - Android 运行时更改的自定义软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958813/

相关文章:

android - 滚动后 ListView 的选中项不存在

iphone - 将返回键更改为 'Continue'

swift - 阻止其他类型的键盘

android - 如何更改android软键盘按键的背景颜色?

android - 隐藏键盘时,Xamarin Forms 按回键不执行任何操作

android - Robotium:如何在 Activity 启动之前更新首选项?

java - 将复选框添加到 ListView 中

ios - 设置包含tableView的collectionView的contentInset

java - 如何通过android自定义键盘操作按钮执行搜索操作?

java - XmlPullParserException : expected: START_TAG <. ..定义