AutoCompleteTextView(输入提示)和自定义键盘

因为最近项目需要使用到输入股票代码下面自动提示股票全名,和方便输入股票用的键盘,所以特地查阅的大量资料总算是搞出来了,特此记录一下方便以后查阅
AutoCompleteTextView系统控件简单使用

<AutoCompleteTextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/autotext"
    />
android:completionHint  设置出现在下拉菜单中的提示标题
android:completionThreshold 设置用户至少输入多少个字符才会显示提示
android:dropDownHorizontalOffset    下拉菜单于文本框之间的水平偏移。默认与文本框左对齐
android:dropDownHeight  下拉菜单的高度
android:dropDownWidth   下拉菜单的宽度
android:singleLine  单行显示
android:dropDownVerticalOffset  垂直偏移量
public class MainActivity extends Activity {
private AutoCompleteTextView autotext;
private ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        autotext =(AutoCompleteTextView) findViewById(R.id.autotext);
        String [] arr={"aa","aab","aac"};
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
        autotext.setAdapter(arrayAdapter);
    }

}

可以实现简单的提示,但是若想实现自定义匹配过滤的规则就必须自定义扩展
1.自定义类继承BaseAdapter且Filterable接口,重写它的两个方法实现过滤规则

自定义股票键盘

package com.gold.hd.sharesdoctor.mvp.view.ui;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import com.gold.hd.sharesdoctor.R;

import java.util.List;

public class KeyBoardNumber {
    private KeyboardView keyboardView;
    private EditText ed;
    private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    public boolean isnun = true;// 是否数据键盘
    public boolean isupper = false;// 是否大写

    public KeyBoardNumber(Activity act, Context ctx, EditText edit) {
        this.ed = edit;
        k1 = new Keyboard(ctx, R.xml.qwerty);
        k2 = new Keyboard(ctx, R.xml.symbols);
        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
        keyboardView.setKeyboard(k2);//打开键盘时默认显示数字键盘
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);//true=打开按键反馈
        keyboardView.setOnKeyboardActionListener(listener);//监听按键
    }

    private OnKeyboardActionListener listener = new 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) {

            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
                hideKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (primaryCode == Keyboard.KEYCODE_SHIFT) { //大小写切换
                changeKey();
                keyboardView.setKeyboard(k1);
            } else if (primaryCode == 000) { //切换到字母
                if (isnun) {
                    isnun = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isnun = true;
                    keyboardView.setKeyboard(k2);
                }
            } else if (primaryCode == 4896) {// 清空
                editable.clear();
            } else if (primaryCode == 57419) { // go left
                if (start > 0) {
                    ed.setSelection(start - 1);
                }
            } else if (primaryCode == 57421) { // go right
                if (start < ed.length()) {
                    ed.setSelection(start + 1);
                }
            } else if (primaryCode == 8150) {
                editable.insert(start, "600");

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

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

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

            } 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 void changeKey() {
        List<Key> keylist = k1.getKeys();
        if (isupper) {//大写切换小写
            isupper = false;
            for (Key key : keylist) {
                if (key.label != null && isword(key.label.toString())) {
                    key.label = key.label.toString().toLowerCase();
                    key.codes[0] = key.codes[0] + 32;
                }
            }
        } else {//小写切换大写
            isupper = true;
            for (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 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:horizontalGap="0.0px"

    android:keyHeight="8%p"
    android:keyWidth="10.000002%p"
    android:verticalGap="0.0px">
    <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="-1"
            android:isModifier="true"
            android:isSticky="true"
            android:keyEdgeFlags="left"
            android:keyIcon="@drawable/sym_keyboard_shift"
            android:keyWidth="14.999998%p" />
        <Key
            android:codes="122"
            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="14.999998%p" />
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key
            android:codes="000"
            android:keyLabel="12#"
            android:keyWidth="20.000004%p" />
        <Key
            android:codes="44"
            android:keyLabel=","
            android:keyWidth="14.999998%p" />
        <Key
            android:codes="32"
            android:isRepeatable="true"
            android:keyIcon="@drawable/sym_keyboard_space"
            android:keyWidth="29.999996%p" />
        <Key
            android:codes="46"
            android:keyLabel="."
            android:keyWidth="14.999998%p" />
        <Key
            android:codes="-3"
            android:keyEdgeFlags="right"
            android:keyLabel="完成"
            android:keyWidth="20.000004%p" />
    </Row>
</Keyboard>

字符键盘

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:keyHeight="8%p"
    android:keyWidth="20%p"
    android:verticalGap="0px">
    <Row>
        <Key
            android:codes="8150"
            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="8160"
            android:keyLabel="000" />
        <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="清空" />

    </Row>
    <Row>
        <Key
            android:codes="8161"
            android:keyLabel="300" />
        <Key
            android:codes="55"
            android:keyLabel="7" />
        <Key
            android:codes="56"
            android:keyLabel="8" />
        <Key
            android:codes="57"
            android:keyLabel="9" />
        <Key
            android:codes="000"
            android:keyLabel="切换"/>
    </Row>
    <Row>
        <Key
            android:codes="8016"
            android:keyLabel="002" />
        <Key
            android:codes="57419"
            android:keyIcon="@drawable/sym_keyboard_left" />
        <Key
            android:codes="48"
            android:keyLabel="0" />
        <Key
            android:codes="57421"
            android:keyIcon="@drawable/sym_keyboard_right" />
        <Key
            android:codes="-3"
            android:isRepeatable="true"
            android:keyLabel="完成" />
    </Row>
</Keyboard>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值