当前位置: 首页 > 工具软件 > TV输入法 > 使用案例 >

TV开发自定义键盘

吴涵育
2023-12-01

定义布局

            <TextView
                android:id="@+id/input_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textSize="18sp"
                android:background="@color/green_title_bar"/>

            <com.open.androidtvwidget.keyboard.SkbContainer
                android:id="@+id/skbContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.open.androidtvwidget.keyboard.SkbContainer>

代码逻辑

@Bind(R.id.skbContainer)
SkbContainer skbContainer;

@Bind(R.id.input_tv)
TextView inputTv;
public void setKeyBoard() {
        skbContainer.setSkbLayout(R.xml.skb_all_key);
        skbContainer.setFocusable(true);
        skbContainer.setFocusableInTouchMode(true);


        // 设置属性(默认是不移动的选中边框)
        setSkbContainerMove();
        //
        skbContainer.setSelectSofkKeyFront(false); // 设置选中边框最前面.
        // 监听键盘事件.
        skbContainer.setOnSoftKeyBoardListener(new SoftKeyBoardListener() {
            @Override
            public void onCommitText(SoftKey softKey) {

                int keyCode = softKey.getKeyCode();
                String keyLabel = softKey.getKeyLabel();
                if (!TextUtils.isEmpty(keyLabel)) { // 输入文字.
                    inputTv.setText(inputTv.getText() + softKey.getKeyLabel());
                } else { // 自定义按键,这些都是你自己在XML中设置的keycode.
                    keyCode = softKey.getKeyCode();
                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        String text = inputTv.getText().toString();
                        if (TextUtils.isEmpty(text)) {
                            Toast.makeText(getApplicationContext(), "文本已空", Toast.LENGTH_LONG).show();
                        } else {
                            inputTv.setText(text.substring(0, text.length() - 1));
                        }
                    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                        finish();
                    } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
                        keyboard.setVisibility(View.GONE);
                    }
                }

            }

            @Override
            public void onBack(SoftKey key) {
                finish();
            }

            @Override
            public void onDelete(SoftKey key) {
                String text = inputTv.getText().toString();
                inputTv.setText(text.substring(0, text.length() - 1));
            }

        });
        // DEMO(测试键盘失去焦点和获取焦点)
        skbContainer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                OPENLOG.D("hasFocus:" + hasFocus);
                if (hasFocus) {
                    if (mOldSoftKey != null)
                        skbContainer.setKeySelected(mOldSoftKey);
                    else
                        skbContainer.setDefualtSelectKey(0, 0);
                } else {
                    mOldSoftKey = skbContainer.getSelectKey();
                    skbContainer.setKeySelected(null);
                }
            }
        });

    }

设置属性

private void setSkbContainerMove() {
        mOldSoftKey = null;
        skbContainer.setMoveSoftKey(true); // 设置是否移动按键边框.
        RectF rectf = new RectF((int) getResources().getDimension(R.dimen.
                w_2), (int) getResources().getDimension(R.dimen.
                h_2), (int) getResources().getDimension(R.dimen.
                w_2), (int) getResources().getDimension(R.dimen.
                h_2));
        skbContainer.setSoftKeySelectPadding(rectf); // 设置移动边框相差的间距.
        skbContainer.setMoveDuration(200); // 设置移动边框的时间(默认:300)
        skbContainer.setSelectSofkKeyFront(true); // 设置选中边框在最前面.
    }

监听键盘按下和松开的事件

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (skbContainer.onSoftKeyDown(keyCode, event))
            return true;

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (skbContainer.onSoftKeyUp(keyCode, event))
            return true;
        return super.onKeyDown(keyCode, event);
    }

键盘的布局放在res/xml下

全键盘:

<?xml version="1.0" encoding="utf-8"?>
<!-- 全键盘 -->
<Keyboard
    bg_res="@color/green_title_bar"
    key_left_padding="@dimen/w_10"
    key_bg_res="@drawable/key_bg_rectangle"
    key_press_res="@mipmap/softkey_bg_press"
    key_select_res="@mipmap/softkey_bg_select2"
    key_text_color="@color/white"
    key_text_size="@dimen/w_30"
    key_top_padding="@dimen/h_10"
    key_height="@dimen/h_70"
    key_width="@dimen/w_70"
    start_pos_x="@dimen/w_10"
    start_pos_y="@dimen/h_20"
    left_right_move="false"
    top_bottom_move="false" >

    <!-- 第一行   key_select_res="@mipmap/softkey_bg_select"-->

    
    <!-- 第二行 -->
    <row>
        <keys
            labels="A|B|C|D|E"
            splitter="|" />
    </row>
	
    <!-- 第三行 -->
    <row>
        <keys
            labels="F|G|H|I|J"
            splitter="|" />
    </row>
	
    <!-- 第四行 -->
    <row>
        <keys
            labels="K|L|M|N|O"
            splitter="|" />
    </row>

    <!-- 第五行 -->
    <row>
        <keys
            labels="P|Q|R|S|T"
            splitter="|" />
    </row>

    <!-- 第六行 -->
    <row>
        <keys
            labels="U|V|W|X|Y"
            splitter="|" />
    </row>

    <row>
        <key
            key_label="Z" />
         <!-- 删除 -->
        <key
            key_code="@string/keycode_del"
            key_icon="@mipmap/delete" />

        <key
            key_code="66"
            key_icon="@mipmap/huiche" />
    </row>
    
</Keyboard>
<?xml version="1.0" encoding="utf-8"?>
<!-- 全键盘-26-字母 -->
<Keyboard
    bg_res="@drawable/main_bg"
    key_bg_res="@drawable/key_bg_rectangle"
    key_height="@dimen/h_100"
    key_left_padding="@dimen/w_10"
    key_select_res="@drawable/white_light_10"
    key_text_color="#b8bbbf"
    key_text_size="@dimen/w_60"
    key_top_padding="@dimen/h_10"
    key_width="@dimen/w_100"
    left_right_move="true"
    qwerty="true"
    qwerty_uppercase="false"
    start_pos_x="@dimen/w_240"
    start_pos_y="@dimen/h_20"
    top_bottom_move="false" >

    <!-- 第一行 -->
    <row>
        <keys
            codes="45|51|33|46|48|53|49|37|43|44"
            labels="Q|W|E|R|T|Y|U|I|O|P"
            splitter="|" />
        <!-- 删除 -->
        <key
            key_code="@string/keycode_del"
            key_icon="@drawable/delete" />
        <!-- 返回-退出输入法 -->
        <key
            key_code="@string/keycode_back"
            key_icon="@drawable/quit_skb" />
    </row>

    <!-- 第二行 -->
    <row>

        <!-- 切换到数字键盘 -->

        <keys
            codes="29|47|32|34|35|36|38|39|40"
            labels="A|S|D|F|G|H|J|K|L"
            splitter="|" />

        <key
            key_icon="@drawable/dou_hao_e"
            key_label="," />
        <key
            key_icon="@drawable/ju_hao_e"
            key_label="." />
        <!-- 回车 -->
        <key
            key_code="@string/keycode_center"
            key_icon="@drawable/g_search_icon"
            key_height="@dimen/h_210"
            key_text_size="@dimen/h_30" />
    </row>

    <!-- 第三行 -->
    <row
        key_bottom_padding="@dimen/h_30">
        <keys
            codes="54|52|31|50"
            labels="Z|X|C|V"
            splitter="|" />
        <!-- 空格 -->
        <key
            key_code="@string/keycode_space"
            key_icon="@drawable/kongge"
            key_width="@dimen/w_210" />

        <keys
            codes="30|42|41"
            labels="B|N|M"
            splitter="|" />
        
        <key
            key_code="@string/keycode_left"
            key_icon="@drawable/key_move_left"/>
         <key
             key_icon="@drawable/key_move_right"
             key_code="@string/keycode_right"/>
    </row>

</Keyboard>

T9键盘

<?xml version="1.0" encoding="utf-8"?>
<!-- T9键盘 -->
<Keyboard
    bg_res="@drawable/main_bg"
    key_left_padding="@dimen/w_10"
    key_bg_res="@drawable/key_bg_rectangle"
    key_select_res="@drawable/white_light_10"
    key_text_color="#b8bbbf"
    key_text_size="@dimen/w_30"
    key_top_padding="@dimen/h_10"
    key_height="@dimen/h_100"
    key_width="@dimen/w_100"
    start_pos_x="@dimen/w_240"
    start_pos_y="@dimen/h_20"
    left_right_move="false"
    top_bottom_move="false" >

    <!-- 第一行 -->
    <row>
        <key
            key_code="1251"
            key_icon="@drawable/t9_key1" />
        <key
            key_code="1252"
            key_icon="@drawable/t9_key2" />
        <key
            key_code="1253"
            key_icon="@drawable/t9_key3" />
    </row>

    <!-- 第二行 -->
    <row>
        <key
            key_code="1254"
            key_icon="@drawable/t9_key4" />
        <key
            key_code="1255"
            key_icon="@drawable/t9_key5" />
        <key
            key_code="1256"
            key_icon="@drawable/t9_key6" />
    </row>

    <!-- 第三行 -->
    <row>
        <key
            key_code="1257"
            key_icon="@drawable/t9_key7" />
        <key
            key_code="1258"
            key_icon="@drawable/t9_key8" />
        <key
            key_code="1259"
            key_icon="@drawable/t9_key9" />
    </row>

    <!-- 第四行 -->
    <row>
        <!-- 删除-->
        <key
            key_code="1260"
            key_icon="@drawable/delete" />
        <key
            key_code="1250"
            key_icon="@drawable/t9_key0" />
        <!-- 清除-->
        <key
            key_code="1261"
            key_icon="@drawable/delete" />
    </row>

</Keyboard>

数字键盘

<?xml version="1.0" encoding="utf-8"?>
<!-- 全键盘-26-字母 -->
<Keyboard
    bg_res="@drawable/main_bg"
    key_bg_res="@drawable/key_bg_rectangle"
    key_height="@dimen/h_100"
    key_left_padding="@dimen/w_10"
    key_press_res="@drawable/softkey_bg_press2"
    key_select_res="@drawable/softkey_bg_select2"
    key_text_color="#b8bbbf"
    key_text_size="@dimen/w_60"
    key_top_padding="@dimen/h_10"
    key_width="@dimen/w_100"
    start_pos_x="@dimen/w_240"
    start_pos_y="@dimen/h_20" >

    <!-- 第一行 -->
    <row>
        <keys
            codes="8|9|10|11|12|13|14|15|16|7"
            labels="1|2|3|4|5|6|7|8|9|0"
            splitter="|" />

        <key
            key_icon="@drawable/ju_hao_e"
            key_label="." />
        <!-- 删除 -->
        <key
            key_code="67"
            key_icon="@drawable/delete" />
        <!-- 退出 -->
        <key
            key_code="4"
            key_icon="@drawable/quit_skb" />
    </row>

    <!-- 第二行 -->
    <row>

        <key
            key_code="1234"
            key_label="abc"
            key_text_size="@dimen/w_40" />
        <key key_label="," />
        <key key_label="。"  />
        <key key_label="!" />
        <key key_label="\?" />
        <key key_label=":" />
        <key key_label=";" />
        <key key_label="&quot;" />
        <key key_label="&quot;" />
        <key key_label="*" />
        <key key_label="(" />
        <key key_label=")" />
        <!-- 回车 -->
         <key
            key_code="@string/keycode_center"
            key_icon="@drawable/g_search_icon"
            key_height="@dimen/h_210"
            key_text_size="@dimen/w_30" />
    </row>

    <!-- 第三行 -->
    <row key_bottom_padding="@dimen/h_30" >
        <key
            key_code="110"
            key_label="翻页"
            key_text_size="@dimen/w_30" />
        
        <key
            key_label=".com"
            key_text_size="@dimen/w_30" />
        <key
            key_label=".CN"
            key_text_size="@dimen/w_30" />
        <key
            key_label="www."
            key_text_size="@dimen/w_30" />
        <key key_label="/" />
        <!-- 空格 -->
        <key
            key_code="@string/keycode_space"
            key_icon="@drawable/kongge"
            key_width="@dimen/w_210" />

        <key key_label="`" />
        <key key_label="~" />
        <key key_label="..." />
        <!-- 光标向左移动 -->
        <key
            key_code="@string/keycode_left"
            key_icon="@drawable/key_move_left"/>
        <!-- 光标向右移动 -->
        <key
            key_code="@string/keycode_right"
            key_icon="@drawable/key_move_right" />
    </row>

</Keyboard>

androidtvwidget 下载链接:https://download.csdn.net/download/ayrascal/10964075

 

 类似资料: