如何更改android软键盘键的背景颜色? [英] How to change background color of key for android soft keyboard?

查看:114
本文介绍了如何更改android软键盘键的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 上实现自定义键盘.我刚刚阅读了有关developer.android.com"的文档并看到了带有软键盘的示例.我所能做的就是改变键盘背景,改变按钮的位置,将 keyIcon 而不是 keyLabel 设置为 key.

I am implementing custom keyboard on Android. I have just read documentation on "developer.android.com" and seen sample with soft keyboard. All I can - it`s to change keyboard background, change placement of buttons, set keyIcon instead of keyLabel to key.

但我仍然无法更改键的背景和颜色.

But I still can not change key`s background and color.

请编写一些 XML 或源代码的示例代码.谢谢!

Please write some sample code of XML or source. Thanks!

我更改背景的示例:

    public class GBInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener{
    ...
    private GBKeyboardView mInputView;
    @Override
        public View onCreateInputView() {
            mInputView = (GBKeyboardView) getLayoutInflater().inflate(R.layout.input, null);
            mInputView.setOnKeyboardActionListener(this);
            mInputView.setKeyboard(mQwertyKeyboard);
            mInputView.setBackgroundResource(R.color.keyboard_background);
            return mInputView;
        }
    ...
    }

我需要这样的东西:

所有按钮的图像 - 这是个坏主意,所以我想找到更好的问题.

Images for all buttons - its bad idea, so I want to find better issue.

推荐答案

将此行代码添加到您的 input.xml

Add this line of code to your input.xml

android:keyBackground="@drawable/samplekeybackground"

因此您的 input.xml 最终应该看起来与此类似.

So your input.xml should end up looking similar to this.

<com.example.keyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:keyPreviewLayout="@layout/input_key_preview"
    android:keyBackground="@drawable/samplekeybackground"
    />

如果你已经有一个很好用的可绘制对象,否则这里是一个示例关键背景可绘制对象,它会产生像你的示例图像一样的结果.

If you already have a drawable to use great otherwise here is a sample key background drawable that will produce results like your example image.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/normal" />
    <!-- Pressed state -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/pressed" /></selector>

如果您想要 xml 中的所有内容,您可以使用形状 xml,如下所示.drawable/normal.xml

If you want everything in xml you can use shape xml's like the following. drawable/normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <stroke android:width="4dp" android:color="#000000" /> 
  <solid android:color="#FFFFFF"/> 
</shape> 

和 drawable/pressed.xml

and drawable/pressed.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <stroke android:width="4dp" android:color="#A3D9F3" /> 
      <solid android:color="#4ABCE8"/> 
    </shape>

这篇关于如何更改android软键盘键的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆