Android popwindow 实现自定义数字键盘

来源:互联网 发布:js 日期不支持 format 编辑:程序博客网 时间:2024/06/03 18:37

**

Android popwindow 实现自定义数字键盘

因项目需要实现了一个自定义的数字输入键盘,特此记录。
只有截图(GIF 不会生成 …….):
这里写图片描述
**
1、键盘源码 KeyBoardView.java

/** * Created by zyj on 2017/6/24. */public class KeyBoardView extends PopupWindow implements View.OnClickListener ,View.OnLongClickListener{    //点击的view    private View clickView;    //    private View conentView;    //按键 1~9 . 完成 清除    private TextView one, two, three, four, five, six, seven, eight, nine, zero, point, equals, clear;    //显示用户当前输入了那些内容    private TextView input_content;    private Activity activity;    //记录键盘的值,默认值是“0”    private String keyValues;    //这个就是默认值...    private static final String DEFAULT = "0";    //这个是可输入的最大长度,根据项目需求修改    private static final int STRING_LENGTH = 7;//    //构造器    public KeyBoardView(Activity activity) {        this.activity = activity;        init();    }    //初始化keyvalues的值为0    private void initKeyValues() {        keyValues = DEFAULT;        refreshView();    }    //进行视图刷新    private void refreshView() {        input_content.setText(keyValues == null || "".equals(keyValues) ? DEFAULT : keyValues);    }    //设置值    //value的值有     //         1~9 .  没什么说的了    //         CLR    长按清除按钮 ,清空keyvalues    //         CL     点击清除按钮 ,删除最后一个      private void setKeyValues(String value) {        if ("CLR".equals(value)){//清空            initKeyValues();            return;        }        if ("CL".equals(value)){            keyValues = keyValues.substring(0,keyValues.length()-1);//把最后一个字符截取调            value="";//把他置为空字符串,这样再继续执行代码时 "CL"就不会影响计算了        }        if (".".equals(value)&&keyValues.indexOf(".")!=-1){            //如果keyvalues中已经有了“.”,就不能再加了                return;        }        //最新值 = 上次的值+输入的值        String tmpValue = keyValues+value;        if (tmpValue.length()>STRING_LENGTH){            //如果超过了最大长度就return            return;        }        if (tmpValue.indexOf(".")!=-1){//那就是小数了            keyValues = tmpValue;//原来的值不动        }else{//整数            int tmpValueInt = Integer.parseInt(tmpValue==null||"".equals(tmpValue)?"0":tmpValue);            keyValues=""+tmpValueInt;        }        refreshView();    }    //初始化    private void init() {        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        conentView = inflater.inflate(R.layout.layout_key_pop, null);        one = (TextView) conentView.findViewById(R.id.one);        input_content = (TextView) conentView.findViewById(R.id.input_content);        two = (TextView) conentView.findViewById(R.id.two);        clear = (TextView) conentView.findViewById(R.id.clear);        three = (TextView) conentView.findViewById(R.id.three);        four = (TextView) conentView.findViewById(R.id.four);        five = (TextView) conentView.findViewById(R.id.five);        six = (TextView) conentView.findViewById(R.id.six);        seven = (TextView) conentView.findViewById(R.id.seven);        eight = (TextView) conentView.findViewById(R.id.eight);        nine = (TextView) conentView.findViewById(R.id.nine);        zero = (TextView) conentView.findViewById(R.id.zero);        point = (TextView) conentView.findViewById(R.id.point);        equals = (TextView) conentView.findViewById(R.id.equal);        one.setOnClickListener(this);        two.setOnClickListener(this);        three.setOnClickListener(this);        four.setOnClickListener(this);        five.setOnClickListener(this);        six.setOnClickListener(this);        seven.setOnClickListener(this);        eight.setOnClickListener(this);        nine.setOnClickListener(this);        zero.setOnClickListener(this);        point.setOnClickListener(this);        equals.setOnClickListener(this);        clear.setOnClickListener(this);        clear.setOnLongClickListener(this);        int h = activity.getWindowManager().getDefaultDisplay().getHeight();//没用上..        int w = activity.getWindowManager().getDefaultDisplay().getWidth();//没用上..        // 设置SelectPicPopupWindow的View        this.setContentView(conentView);        // 设置SelectPicPopupWindow弹出窗体的宽//        this.setWidth(w);        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);        // 设置SelectPicPopupWindow弹出窗体的高        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);        // 设置SelectPicPopupWindow弹出窗体可点击        this.setFocusable(true);        this.setOutsideTouchable(true);        // 刷新状态        this.update();        // 实例化一个ColorDrawable颜色为半透明        ColorDrawable dw = new ColorDrawable(0000000000);        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作        this.setBackgroundDrawable(dw);        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);        // 设置SelectPicPopupWindow弹出窗体动画效果//        this.setAnimationStyle(R.style.AnimationToast);    }    public void showPopupWindow(View parent) {        if (!this.isShowing()) {            clickView = parent;            initKeyValues();//            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);            this.showAsDropDown(parent);        } else {            this.dismiss();        }    }    public void dismissPopWindow() {        if (isShowing()) {            dismiss();        }    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.one:setKeyValues("1");break;            case R.id.two:setKeyValues("2");                break;            case R.id.three:setKeyValues("3");                break;            case R.id.four:setKeyValues("4");                break;            case R.id.five:setKeyValues("5");                break;            case R.id.six:setKeyValues("6");                break;            case R.id.seven:setKeyValues("7");                break;            case R.id.eight:setKeyValues("8");                break;            case R.id.nine:setKeyValues("9");                break;            case R.id.zero:setKeyValues("0");                break;            case R.id.point:setKeyValues(".");                break;            case R.id.equal:                dismissPopWindow();                if (keyInputListener!=null){                //如果用户最后输入.时 比如 12345. ,这时候点击“完成”我们返回的字符串就是"12345.",无论是显示还是计算都是不好看的,我这里的处理是在末尾补0,如果项目需求不是可以将此删除掉                    if (keyValues.indexOf(".")==keyValues.length()-1){                        keyValues = keyValues+"0";                    }else{                        if (keyValues.indexOf(".")!=-1){                            //是double                            double value = Double.parseDouble(keyValues);                            if (value==0d){                                keyValues = "0.0";                            }                        }                    }                    keyInputListener.onFinish(clickView,keyValues);                }                break;            case R.id.clear:                setKeyValues("CL");//删除最后一个元素                break;        }    }    @Override    public boolean onLongClick(View v) {        setKeyValues("CLR");//长按清除        return true;//只执行长按事件    }    //这个是自定义一个监听器,主要是用于当用户点完成将输入的数据返回    public interface OnKeyInputListener{        //当用户点击“完成”时会调用此方法        //参数view:这个是你在activity点击的控件的对象        //参数value:这个就是keyValues值        public void onFinish(View view, String value);    }    private OnKeyInputListener keyInputListener;    //监听器Getter    public OnKeyInputListener getKeyInputListener() {        return keyInputListener;    }    //监听器Setter    public void setKeyInputListener(OnKeyInputListener keyInputListener) {        this.keyInputListener = keyInputListener;    }}

2、键盘布局 layout_key_pop.xml

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/key_layout_bg"    android:columnCount="3"    android:orientation="horizontal"    android:rowCount="6">        <TextView            android:id="@+id/input_content"            android:layout_width="244dip"            android:layout_height="60dip"            android:layout_marginLeft="5dip"            android:textSize="30dip"            android:text="1234567889"            android:textStyle="bold|italic"            android:layout_columnSpan="3"            android:layout_gravity="fill"            android:paddingRight="5dip"            android:gravity="center|right" />    <TextView        android:id="@+id/one"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="1" />          <TextView        android:id="@+id/two"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="2" />           <TextView        android:id="@+id/three"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="3" />          <TextView        android:id="@+id/four"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="4" />          <TextView        android:id="@+id/five"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="5" />          <TextView        android:id="@+id/six"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="6" />          <TextView        android:id="@+id/seven"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="7" />          <TextView        android:id="@+id/eight"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="8" />          <TextView        android:id="@+id/nine"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="9" />          <TextView        android:id="@+id/point"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="." />    <TextView        android:id="@+id/zero"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="0" />    <TextView        android:id="@+id/clear"        style="@style/keyStyle"        android:layout_width="120dip"        android:layout_height="60dip"        android:text="清除" />    <TextView        android:id="@+id/equal"        style="@style/keyStyle"        android:layout_height="70dip"        android:layout_columnSpan="3"        android:layout_gravity="fill"        android:background="@drawable/key_selector_func"        android:text="完成"        android:textColor="#fff"/></GridLayout>

style.xml文件

 <style name="keyStyle">        <item name="android:background">@drawable/key_selector_nums</item>        <item name="android:layout_margin">2dip</item>        <item name="android:textSize">20dip</item>        <item name="android:gravity">center</item>    </style>

注:所有的drawable文件我就不细说了,就是个样式。


4、使用

 private void OnClick(View view) {        switch (view.getId()) {            case R.id.text1:                //控件的点击事件里显示键盘                keyBoardView.showPopupWindow(view);                break;        }    }
KeyBoardView keyBoardView;//onCreate中keyBoardView = new KeyBoardView(this);keyBoardView.setKeyInputListener(new KeyBoardView.OnKeyInputListener() {            @Override            public void onFinish(View view, String value) {                switch (view.getId()){                    case R.id.text1:                        text1.setText(value);                        break;                    case R.id.text2:                        text2.setText(value);                        break;                }            }        });