android 自定义键盘,代码实现自定义属性(自定义键盘背景,各个键的背景等)

时间:2015-12-19 19:15:44   收藏:0   阅读:3940

由于工作需要,所以接触了自定义键盘。但是发现自己写的键盘太过丑陋了一些。废话不多说,先上图

技术分享   技术分享

第一张是修改后的。第二张是修改钱的。这基本上就是

OK。接下来就是重点了。

android的keyboardview的属性中是有keybackground 的,但是在使用的时候,却发现没有生效。仔细看了下源码才发现。下面的这句话,把属性集合给设置成了空。所以就键盘的属性就一直无法生效。

KeyboardView mKeyboardView = new KeyboardView(this, null);  

悲剧发生了。就要想解决办法。这里既然知道是属性集合被置空了。那么就设置属性集合被。查阅api发现。方法挺多的。就选用了其中一种方法。

    XmlPullParser parser =activity.getResources().getXml(R.layout.keyboardview);
         AttributeSet attributes = Xml.asAttributeSet(parser);
         
         int type;  
        try{  
            while ((type = parser.next()) != XmlPullParser.START_TAG &&  
                    type != XmlPullParser.END_DOCUMENT) {  
                // Empty  
            }  

            if (type != XmlPullParser.START_TAG) {  
                Log.e("","the xml file is error!\n");  
            }     
        } catch (XmlPullParserException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        Log.d("",""+parser.getAttributeCount());  
        KeyboardView mKeyboardView = new KeyboardView(this, attributes);
        mKeyboardView.setFocusable(true);
        mKeyboardView.setFocusableInTouchMode(true);
        mKeyboardView.setId(R.id.keyboard_view);
        mKeyboardView.setVisibility(View.GONE);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        RelativeLayout rlyt = new RelativeLayout(this);
        rlyt.addView(mKeyboardView, lp);
keyboardview.xml 中存放的是键盘keyboardview的属性集合。在这个地方设置集合不怕不会生效的。
<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/lightblack"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyBackground="@drawable/btn_keyboard_key"
    android:keyTextColor="@color/white"
    android:visibility="gone" />

就是这样的实现了键盘的自定义皮肤。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!