自定义股票键盘

在开发股票App的时候可能会被要求使用股票键盘,前一阵从不炒股得人第一次听说了股票键盘,后来查阅了一下,其实就是自定义的键盘,奈何一只搜索股票键盘,难住了我,所以我写了一个股票键盘。

话不多说直接上马了:

首先要先写一个布局(就是在要显示键盘的类的布局里):

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        >
            <android.inputmethodservice.KeyboardView
                android:id="@+id/hot_keyboard_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/black"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:keyTextColor="@color/white"
                android:keyPreviewLayout="@layout/key_preview_layout"

                />
    </RelativeLayout>
其中 KeyboardView键盘的控件。

接下来写一个键盘类:

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.List;

import cn.uniwa.uniwa.R;

public class KeyBoardNumber {
    private KeyboardView keyboardView;
    private Keyboard k1;// 英文键盘
    private Keyboard k2;// 数字键盘
    private EditText ed;
    boolean b = true;
    Activity a;//接收传过来Activity
    LinearLayout ll;
    ImageView hotIvDel;
    LinearLayout hotLlResult;
    RelativeLayout hotRlNoFind;

    public KeyBoardNumber(Activity act, Context ctx, EditText edit) {//Activity可以将使用到此键盘的类传过来,也可以删除不传
        this.ed = edit;
        a = act;
        k1 = new Keyboard(ctx, R.xml.letter);
        k2 = new Keyboard(ctx, R.xml.symbols);
        keyboardView = (KeyboardView) act.findViewById(R.id.hot_keyboard_view);
        keyboardView.setKeyboard(k2);
        //软键盘是否启用
        keyboardView.setEnabled(true);
        //按键提示是否启用
        keyboardView.setPreviewEnabled(false);

        keyboardView.setOnKeyboardActionListener(listener);
        List<Keyboard.Key> keylist = k1.getKeys();

        for (Keyboard.Key key : keylist) {
            if (key.label != null && isword(key.label.toString())) {
                key.label = key.label.toString().toUpperCase();
                key.codes[0] = key.codes[0] - 32;
            }
        }
    }

    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void swipeUp() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {//注意!设置的键盘按钮可以再此方法内设置
            ll = (LinearLayout) a.findViewById(R.id.hot_ll_text);
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
//                Toast.makeText(KeyBoardNumber.this,"ok+"+editable,Toast.LENGTH_LONG).show();
//                hideKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
//                        hotIvDel.setVisibility(View.GONE);
//                        hotLlResult.setVisibility(View.GONE);
//                        ll.setVisibility(View.VISIBLE);
//                        hotRlNoFind.setVisibility(View.GONE);
                    }
                }
            } else if (primaryCode == -15) {
                EditText ed = (EditText) a.findViewById(R.id.hot_et_search);//这些方法用不到可以删除,这是从使用键盘的activity传来的id
                hotLlResult = (LinearLayout) a.findViewById(R.id.hot_ll_result);
                hotRlNoFind = (RelativeLayout) a.findViewById(R.id.hot_rl_noFind);
                String text = ed.getText().toString();
               if (text.equals("")||text == null){
                   hotIvDel.setVisibility(View.GONE);
                   hotLlResult.setVisibility(View.GONE);
                   ll.setVisibility(View.VISIBLE);
                   hotRlNoFind.setVisibility(View.GONE);
               }
                hideKeyboard();
//                ll.setVisibility(View.VISIBLE);
//                ed.setText("");
            } else if (primaryCode == 4896) {// 清空
                editable.clear();
            } else if (primaryCode == 002) {
                editable.insert(start, "002");

            } else if (primaryCode == 300) {
                editable.insert(start, "300");

            } else if (primaryCode == 600) {
                editable.insert(start, "600");

            } else if (primaryCode == 601) {
                editable.insert(start, "601");

            } else if (primaryCode == 000) {
                editable.insert(start, "000");

            } else if (primaryCode == 115) {
                editable.insert(start, "S");
            } else if (primaryCode == 8016) {
//				editable.insert(start, "16");
                if (b) {
                    keyboardView.setKeyboard(k1);
                    b = false;
                } else {
                    keyboardView.setKeyboard(k2);
                    b = true;
                }

            } else {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    public boolean isKeyBoardShow() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            return false;
        } else {
            return true;
        }

    }

    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    //隐藏键盘
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.INVISIBLE);
        }
    }

    private boolean isword(String str) {
        String wordstr = "abcdefghijklmnopqrstuvwxyz";
        if (wordstr.indexOf(str.toLowerCase()) > -1) {
            return true;
        }
        return false;
    }


注意看我写的注释应该就能明白了


接下来是英文键盘:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p" android:horizontalGap="0px"
    android:verticalGap="0px" android:keyHeight="8%p">
    <Row>
        <Key android:codes="113" android:keyEdgeFlags="left" android:keyLabel="Q"/>
        <Key android:codes="119" android:keyLabel="W"/>
        <Key android:codes="101" android:keyLabel="E"/>
        <Key android:codes="114" android:keyLabel="R"/>
        <Key android:codes="116" android:keyLabel="T"/>
        <Key android:codes="121" android:keyLabel="Y"/>
        <Key android:codes="117" android:keyLabel="U"/>
        <Key android:codes="105" android:keyLabel="I"/>
        <Key android:codes="111" android:keyLabel="O"/>
        <Key android:codes="112" android:keyEdgeFlags="right" android:keyLabel="P"/>
    </Row>
    <Row>
        <Key android:codes="97"   android:horizontalGap="4.999995%p" android:keyEdgeFlags="left" android:keyLabel="A"/>
        <Key android:codes="115" android:keyLabel="S"/>
        <Key android:codes="100" android:keyLabel="D"/>
        <Key android:codes="102" android:keyLabel="F"/>
        <Key android:codes="103" android:keyLabel="G"/>
        <Key android:codes="104" android:keyLabel="H"/>
        <Key android:codes="106" android:keyLabel="J"/>
        <Key android:codes="107" android:keyLabel="K"/>
        <Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="L"/>

    </Row>
    <Row>
        <Key android:codes="122" android:horizontalGap="2%p" android:keyEdgeFlags="left" android:keyLabel="Z"/>
        <Key android:codes="120" android:keyLabel="X"/>
        <Key android:codes="99" android:keyLabel="C"/>
        <Key android:codes="118" android:keyLabel="V"/>
        <Key android:codes="98" android:keyLabel="B"/>
        <Key android:codes="110" android:keyLabel="N"/>
        <Key android:codes="109"  android:keyLabel="M"/>
        <Key android:codes="-5"
            android:isRepeatable="true"
            android:keyEdgeFlags="right"
            android:keyIcon="@drawable/sym_keyboard_delete"
            android:keyWidth= "25%p"/>
    </Row>
    <Row>
        <Key android:codes="8016"
            android:keyLabel="123"
            android:keyEdgeFlags="left"
            android:keyWidth= "25%p"/>
        <Key android:codes="4896"
            android:isRepeatable="true"
            android:keyWidth= "25%p"
            android:keyLabel="@string/key_clear" />
        <Key android:codes="-15"
            android:isRepeatable="true"
            android:keyWidth= "25%p"
            android:keyLabel="隐藏" />
        <Key android:codes="-3"
            android:isRepeatable="true"
            android:keyEdgeFlags="right"
            android:keyLabel="@string/key_ok"
            android:keyWidth= "25%p" />

    </Row>
    </Keyboard>

数字键盘:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
	android:keyWidth="20%p" android:horizontalGap="0px"
	android:verticalGap="0px" android:keyHeight="8%p"
	>
	<Row>
	    <Key android:codes="600" android:keyLabel="600" />
		<Key android:codes="49" android:keyLabel="1" />
		<Key android:codes="50" android:keyLabel="2" />
		<Key android:codes="51" android:keyLabel="3" />
		<Key android:codes="-5" android:isRepeatable="true" 
		    android:keyIcon="@drawable/sym_keyboard_delete" />
		
	</Row>
	<Row>
	    <Key android:codes="601" android:keyLabel="601" />
		<Key android:codes="52" android:keyLabel="4" />
		<Key android:codes="53" android:keyLabel="5" />
		<Key android:codes="54" android:keyLabel="6" />
		<Key android:codes="4896" android:keyLabel="@string/key_clear" />
		
	</Row>
	<Row>
	    <Key android:codes="000" android:keyLabel="000" />
		<Key android:codes="55" android:keyLabel="7" />
		<Key android:codes="56" android:keyLabel="8" />
		<Key android:codes="57" android:keyLabel="9" />
		<Key android:codes="-15" android:isRepeatable="true"
			android:keyLabel="隐藏" />
	</Row>
	<Row>
	    <Key android:codes="8016" android:keyLabel="ABC" />
	    <Key android:codes="002" android:keyLabel="002" />
		<Key android:codes="48" android:keyLabel="0" />		
		<Key android:codes="300" android:keyLabel="300" />
		<Key android:codes="-3" android:isRepeatable="true"
			android:keyEdgeFlags="right"
			android:keyLabel="@string/key_ok"/>
	</Row>
</Keyboard>

两个键盘内的功能键的codes都是重复的,可以进行数字英文进行切换

以上键盘设置完毕,只要在Activity中使用即可:

private KeyBoardNumber keyBoard;
keyBoard = new KeyBoardNumber(act, mContext, hotEtSearch);

 hotEtSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int inputback = hotEtSearch.getInputType();
//                hotEtSearch.setInputType(InputType.TYPE_NULL);
                keyBoard.showKeyboard();
                hotEtSearch.setInputType(inputback);
                return false;
            }
        });
这样就会显示键盘了;

但还是要注意,键盘布局的显示和隐藏;

如果有问题可以私聊我,会尽力帮忙。。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值