Android 自定义KeyboardView的键盘背景色

  • Post author:
  • Post category:其他


1,

  <com.gs.keyboard.MyKeyboardView
        android:id="@+id/keyboard_view"
        android:layout_width="fill_parent"
        android:layout_height="236dip"
        android:layout_alignParentRight="true"
        android:background="#ebebeb"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:keyBackground="@drawable/keyboard_key"
        android:keyTextColor="#000000"
        android:paddingBottom="10dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="40dip"
        android:shadowRadius="0.0"
        android:textStyle="normal"
        />

其中keyBackground就是关于按键背景的定义

2,keyboard_key配置文件

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/keyboard_key_bg_c"/>
    <item android:drawable="@drawable/keyboard_key_bg"/>
</selector>

//keyboard_key_bg_c:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#ffffffff" />

            <stroke android:width="1.0dip" android:color="#ff888888" />

            <corners android:radius="4.0dip"/>
        </shape>
    </item>

</selector>

//keyboard_key_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@android:color/darker_gray" />

            <stroke android:width="1.0dip" android:color="#ffffff" />

            <corners android:radius="4.0dip"/>
        </shape>
    </item>

</selector>

3,如果我们需要单独修改某个按钮的背景:

for (Key key : mKeyboardNumber.getKeys()) {
    //Log.d("SecurityKeyboard", "key.label:" + key.label);
    if (/*key.codes[0] == 49*/key.label != null && key.label.toString().equals("1") ) {//设置你需要的按键
        //key.onPressed();
        key.pressed = true;

    }else {
       // key.pressed = false;
    }
}



版权声明:本文为PZ0605原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。