TV开发自定义键盘

定义布局

            <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.setKeySelecte
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值