创新实训——第二周

    根据我们小组内的分工,本周我完成的内容为一个简单自定义数字键盘的实现。


    内容包括两个java文件,分别控制实现了页面布局和自定义键盘构造及监听。number.xml中编写了数字键盘的内容。

number.xml文件:数字键盘1-9、删除键、完成键:

<Row>
    <Key android:codes="49" android:keyLabel="1" />
    <Key android:codes="50" android:keyLabel="2" />
    <Key android:codes="51" android:keyLabel="3" />
</Row>

<Row>
    <Key android:codes="52" android:keyLabel="4" />
    <Key android:codes="53" android:keyLabel="5" />
    <Key android:codes="54" android:keyLabel="6" />
</Row>

<Row>
    <Key android:codes="55" android:keyLabel="7" />
    <Key android:codes="56" android:keyLabel="8" />
    <Key android:codes="57" android:keyLabel="9" />
</Row>

<Row>
    <Key android:codes="-5"  android:keyIcon="@drawable/del" /> 
    <Key android:codes="48" android:keyLabel="0" />
    <Key android:codes="-3" android:keyLabel="完成" />
</Row>

布局文件的编写,包括一个文本输入框和自定义键盘

<EditText
    android:id="@+id/my_editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white"/>


<android.inputmethodservice.KeyboardView
    android:id="@+id/keyboard_view_my"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyPreviewHeight="@dimen/x280"
    android:keyPreviewLayout="@layout/key_preview_layout"
    android:keyPreviewOffset="@dimen/x1"
    android:visibility="invisible"
    tools:visibility="visible" />

NumboardUtil中实现了Numboard的构造方法,因为在未来我们需要实现的是将手写字母的图片输出在文本编辑框内,所以在此我传入了Resourses参数,便于获得存在系统中的资源文件。

public NumboardUtil(Activity act, EditText editText, KeyboardView keyboardViews, Resources resourses) {
    this.res = resourses;
    this.ed = editText;
    this.keyboardView = keyboardViews;
    k = new Keyboard(act.getApplication(), R.xml.number);//传入的number决定了键盘
    keyboardView.setKeyboard(k);
    keyboardView.setEnabled(true);
    keyboardView.setPreviewEnabled(false);//是否显示隐藏点击pop布局bufen
    keyboardView.setOnKeyboardActionListener(listener);
}

接下来编写一个监听器继承自OnKeyboardActionListener,用于监听键盘动作并针对不同动作有不同的实现,在OnKey方法中我们将图片放入文本框中,这些图片将来即为存储的用户手写体图片。

public void onKey(int i, int[] ints) {
    Editable editable = ed.getText();
    int start = ed.getSelectionStart();
    if (i == Keyboard.KEYCODE_DELETE) {// 回退
        if (editable != null && editable.length() > 0) {
            if (start > 0) {
                editable.delete(start - 1, start);
            }
        }
    } else if (i == Keyboard.KEYCODE_CANCEL) {// 完成
        hideKeyboard();
    } else { //将要输入的数字输出在编辑框中
        Drawable drawable = res.getDrawable(R.drawable.keyboard_backspace);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight());
        SpannableString spannable = new SpannableString(editable
                .toString() + " ");
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spannable.setSpan(span,editable.length(),
                editable.length() + " ".length(),
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        ed.append(spannable);
        //ed.setText(spannable);
    }
}

最后在MainActivity中调用NumboardUtil类,并将系统键盘隐藏,再让自定义的键盘仅在点击了文本框后出现。

public void hideSystemKeyBoard() {
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            numboardUtil.hideKeyboard();
        }
    }
    return super.onTouchEvent(event);
}





猜你喜欢

转载自blog.csdn.net/weixin_38307753/article/details/80044980