Android 自定义KeyboardView的键盘背景色

本文详细介绍了如何在Android中自定义虚拟键盘的按键背景,包括使用XML定义按键样式,以及如何通过代码为特定按键设置不同的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值