创新实训——第二周1

    本周我首先在上周的键盘基础上进行了改进,增加了字母键盘,基本完成了自定义键盘,包括数字键盘、符号键盘和字母键盘之间的转换,以及字母键盘的大小写等功能。

    字母键盘的xml文件,用来对键盘的排列键盘布局:

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10%p" android:keyHeight="60dp"
    android:horizontalGap="0.0px" android:verticalGap="0.0px"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <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:horizontalGap="4.999995%p" android:codes="97"
            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:keyWidth="14.999998%p" android:codes="-1"
            android:keyEdgeFlags="left" android:isModifier="true"
            android:isSticky="true" android:keyIcon="@drawable/keyboard_shift" />
        <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:keyWidth="14.999998%p" android:codes="-5"
            android:keyEdgeFlags="right" android:isRepeatable="true"
            android:keyIcon="@drawable/keyboard_delete" />
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:keyWidth="20.000004%p" android:codes="-2"
            android:keyLabel="12#" />
        <Key android:keyWidth="14.999998%p" android:codes="44"
            android:keyLabel="," />
        <Key android:keyWidth="29.999996%p" android:codes="32"
            android:isRepeatable="true" android:keyIcon="@drawable/keyboard_space" />
        <Key android:keyWidth="14.999998%p" android:codes="46"
            android:keyLabel="." />
        <Key android:keyWidth="20.000004%p" android:codes="-3"
            android:keyEdgeFlags="right" android:keyLabel="完成" />
    </Row>
</Keyboard>

    然后是对上周的自定义键盘类的改进,首先是KeyboardUtil类的变量和构造方法

    private KeyboardView keyboardView;
    private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    private Keyboard k3;//符号键盘
    public boolean isnun = false;// 是否数据键盘
    public boolean isupper = false;// 是否大写
    private boolean isSymbol = false;// 是否是符号
  float sensorData;//抖动参数
  int millisecond;//触键时间 毫秒
  float skewData;//倾斜程度
  public static final int MY_ROW = 100;
  public static final int MY_COL = 75;
  public static List<Bitmap> inputBmpList = new ArrayList<Bitmap>();//存放文本框中已输入的图片list
  public static int count;//文本框中已输入的图片个数
public KeyboardUtil(Activity act, EditText editText, KeyboardView keyboardViews, Resources resourses, int[][][] bitmap) {
        this.act = act;
        this.ed = editText;
        this.res = resourses;
        this.imageBmp = bitmap;
        this.A = bitmap[0];
        k1 = new Keyboard(act.getApplication(), R.xml.qwerty);
        k2 = new Keyboard(act.getApplication(), R.xml.number);
        k3 = new Keyboard(act.getApplication(), R.xml.symbol);
        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view_my);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(listener);
        mToast = Toast.makeText(act, "", Toast.LENGTH_SHORT);
    }


下面为为了实现大小写转换定义的方法,isupper是一个布尔变量,来决定大小写,然后通过对key的label的重写和codes的重定义来完成大小写切换。

private void changeKey() {
    List<Keyboard.Key> keylist = k1.getKeys();
    if (isupper) {//大写切换小写
        isupper = false;
        for(Keyboard.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(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;
            }
        }
    }
}

    在继承自OnKeyboardActionListener的键盘监听器中,主要对OnKey方法进行了重写,实现了键盘的退格、切换大小写以及数字键盘和字母键盘切换的功能,都是通过对按键的codes值的判断实现的。小组其他同学已经完成了个人字体库的存储类,在将对应的字母输入到文本框中时,只需显示这些键盘对应的用户字体图片。

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            Bitmap bmp;

            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);
                        inputBmpList.remove(start-1);
                        count --;
                    }
                }
            } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
                changeKey();
                keyboardView.setKeyboard(k1);

            } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
                if (isnun) {
                    isnun = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isnun = true;
                    keyboardView.setKeyboard(k2);
                }
            }else if (primaryCode == 90001) {
//                  字母与符号切换
                if (isSymbol) {
                    isSymbol = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isSymbol = true;
                    keyboardView.setKeyboard(k3);
                }
            } 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 == 32){//空格符
                bmp = Bitmap.createBitmap(MY_COL/2,MY_ROW, Bitmap.Config.ARGB_8888);
                System.out.println(sensorData);
                //iv1.setImageBitmap(bmp);
                SpannableString ss=getBitmapMime(bmp);
                insertPhotoToEditText(ss);
                count++;

                inputBmpList.add(start,bmp);
            }
            else {
                if(imageBmp[primaryCode-33]!=null){
                    bmp = paintNewImage(imageBmp[primaryCode-33],sensorData,millisecond,skewData);
                }else{
                    bmp = Bitmap.createBitmap(MY_COL/2,MY_ROW, Bitmap.Config.ARGB_8888);
                    //提示字体库不全
                    mToast.setText("个人字体库不完整!");
                    mToast.show();
                }
                System.out.println(sensorData);
                SpannableString ss=getBitmapMime(bmp);
                insertPhotoToEditText(ss);
                inputBmpList.add(start,bmp);
                count++;
            }
        }

在文本框中显示图片的方法,通过SpannableString和ImageSpan类来实现。

private SpannableString getBitmapMime(Bitmap bmp)
    {
        SpannableString ss=new SpannableString(" ");
        Drawable drawable = new BitmapDrawable(bmp);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(imageSpan,0," ".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

/**
     * 将ss插入文本框当前光标位置,并移动光标
     * */
    private void insertPhotoToEditText(SpannableString ss)
    {
        Editable et=ed.getText();
        int start=ed.getSelectionStart();
        et.insert(start,ss);
        ed.setText(et);
        ed.setSelection(start+ss.length());
        ed.setFocusableInTouchMode(true);
        ed.setFocusable(true);
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值