keyboard键盘demo

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:background="@color/lightblack"
            android:keyBackground="@drawable/btn_keyboard_key"
            android:keyTextColor="@color/white"
            android:keyPreviewLayout="@layout/key_preview_layout"
            android:visibility="gone" />
    </RelativeLayout>

</LinearLayout>

key_preview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="@color/font_red"
android:gravity="center"
android:background="@color/blue"/>

qwerty.xml

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"
    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/sym_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/sym_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/sym_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>

symbols.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="25%p" android:horizontalGap="0px"
    android:verticalGap="0px" android:keyHeight="@dimen/key_height">
    <Row>
        <Key android:codes="49" android:keyLabel="1" />
        <Key android:codes="50" android:keyLabel="2" />
        <Key android:codes="51" android:keyLabel="3" />
        <Key android:codes="57419" android:keyEdgeFlags="right"
            android:keyIcon="@drawable/sym_keyboard_left" />
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4" />
        <Key android:codes="53" android:keyLabel="5" />
        <Key android:codes="54" android:keyLabel="6" />
        <Key android:codes="57421" android:keyEdgeFlags="right"
            android:keyIcon="@drawable/sym_keyboard_right" />
    </Row>
    <Row>
        <Key android:codes="55" android:keyLabel="7" />
        <Key android:codes="56" android:keyLabel="8" />
        <Key android:codes="57" android:keyLabel="9" />
        <Key android:codes="-3" android:keyHeight="100dip"
            android:keyEdgeFlags="right" android:isRepeatable="true"
            android:keyLabel="完成" />
    </Row>
    <Row>
        <Key android:codes="-2" android:keyLabel="ABC" />
        <Key android:codes="48" android:keyLabel="0" />
        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
    </Row>
</Keyboard>

btn_keyboard_key.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/normal_key_hl_bg" />
    <item android:drawable="@drawable/normal_key_bg" />
</selector>

KeydemoActivity

package cn.key;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

public class KeydemoActivity extends Activity {
    private Context ctx;
    private Activity act;
    private EditText edit;
    private EditText edit1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ctx = this;
        act = this;

        edit = (EditText) this.findViewById(R.id.edit);
        edit.setInputType(InputType.TYPE_NULL);

        edit1 = (EditText) this.findViewById(R.id.edit1);

        edit.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                new KeyboardUtil(act, ctx, edit).showKeyboard();
                return false;
            }
        });

        edit1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inputback = edit1.getInputType();
                edit1.setInputType(InputType.TYPE_NULL);
                new KeyboardUtil(act, ctx, edit1).showKeyboard();
                edit1.setInputType(inputback);
                return false;
            }
        });

    }
}

KeyboardUtil

package cn.key;

import java.util.List;

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

public class KeyboardUtil {
    private Context ctx;
    private Activity act;
    private KeyboardView keyboardView;
    private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    public boolean isnun = false;// 是否数据键盘
    public boolean isupper = false;// 是否大写

    private EditText ed;

    public KeyboardUtil(Activity act, Context ctx, EditText edit) {
        this.act = act;
        this.ctx = ctx;
        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(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(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 == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
                if (isnun) {
                    isnun = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isnun = true;
                    keyboardView.setKeyboard(k2);
                }
            } 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 {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    /**
     * 键盘大小写切换
     */
    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;
                }
            }
        }
    }

    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;
    }

}
时间: 2024-10-05 07:59:10

keyboard键盘demo的相关文章

android开发(45) 自定义软键盘(输入法)

概述 在项目开发中遇到一个需求,”只要数字键盘的输入,仅仅有大写字母的输入,某些输入法总是会提示更新,弹出广告等“,使得我们需要自定义输入. 关联到的知识 KeyboardView      一个视图对象,展示了键盘.它需要关联到一个 Keyboard对象才能展示. Keyboard              键盘对象,通过加载xml的配置获得键盘的排列. xml 文件键盘描述     一个xml文件,放置在 xml 资源文件夹下,描述了 显示的键盘按钮,和排列,键盘宽度和高度等. 具体实现 准

android:configChanges=&quot;keyboard|keyboardHidden|orientation|screenSize&quot;

<activity android:name="xxxActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" android:launchMode="singleTask" ---launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时

github上老外做的jQuery虚拟键盘

jQuery官方比较迟滞的更新版本: http://plugins.jquery.com/keyboard/ 最新版本更新官方地址: https://github.com/Mottie/Keyboard/wiki demo地址: https://mottie.github.io/Keyboard/ 原文地址:https://www.cnblogs.com/BluceLee/p/9149674.html

计算机常用英语词汇

计算机常用英语词汇一.硬件类(Hardware)二.软件类(Software)三.网络类(Network)四.其它CPU(Center Processor Unit)中央处理单元Main board主板RAM(random access memory)随机存储器(内存)ROM(Read Only Memory)只读存储器Floppy Disk软盘Hard Disk硬盘CD-ROM光盘驱动器(光驱)Monitor监视器Keyboard键盘Mouse鼠标Chip芯片CD-R光盘刻录机HUB集线器Mo

Jcrop最新手册

目录 Jcrop简介 Jcrop 是一个功能强大的 jQuery 图像裁剪插件,结合后端程序(例如:PHP)可以快速的实现图片裁剪的功能. Jcrop是一款免费的软件,采用MIT License发布. 注:本文主要围绕 Jcrop v2.0.0-RC1 版本进行介绍,一些参数或API在 Jcrop v0.9.12 及之前版本中可能并不支持. 版本 Jcrop v0.9.12 支持画一个框截取图片. Jcrop v2.0.0-RC1 相比于老版本,新增了支持画多框的功能. 下载地址: http:/

IT常用英语

CPU(Center Processor Unit)中央处理单元 mainboard主板 RAM(random access memory)随机存储器(内存) ROM(Read Only Memory)只读存储器 Floppy Disk软盘 Hard Disk硬盘 CD-ROM光盘驱动器(光驱) monitor监视器 keyboard键盘 mouse鼠标 chip芯片 CD-R光盘刻录机 HUB集线器 Modem= MOdulator-DEModulator,调制解调器 P-P(Plug and

MATLAB命令大全

一.常用对象操作:除了一般windows窗口的常用功能键外.1.!dir 可以查看当前工作目录的文件. !dir& 可以在dos状态下查看.2.who 可以查看当前工作空间变量名, whos 可以查看变量名细节.3.功能键:功能键 快捷键 说明方向上键 Ctrl+P 返回前一行输入方向下键 Ctrl+N 返回下一行输入方向左键 Ctrl+B 光标向后移一个字符方向右键 Ctrl+F 光标向前移一个字符Ctrl+方向右键 Ctrl+R 光标向右移一个字符Ctrl+方向左键 Ctrl+L 光标向左移

[转] MATLAB快捷键

原文地址:MATLAB快捷键大全 (转载)作者:掷地有声 一.索引混排版 备注:删除了如F1(帮助)等类型的常见快捷命令 SHIFT+DELETE永久删除 DELETE删除 ALT+ENTER属性 ALT+F4关闭 CTRL+F4关闭 ALT+TAB切换 ALT+ESC切换 ALT+空格键窗口菜单 CTRL+ESC开始菜单 拖动某一项时按CTRL复制所选项目 拖动某一项时按CTRL+SHIFT创建快捷方式 将光盘插入到CD-ROM驱动器时按SHIFT键阻止光盘自动播放 Ctrl+1,2,3...

matlab快捷键大全

原文地址,点此查看 一.常用对象操作 除了一般windows窗口的常用功能键外. 1.!dir 可以查看当前工作目录的文件. !dir& 可以在dos状态下查看. 2.who   可以查看当前工作空间变量名, whos 可以查看变量名细节. 3.功能键: 功能键           快捷键           说明 方向上键      Ctrl+P       返回前一行输入 方向下键      Ctrl+N       返回下一行输入 方向左键      Ctrl+B       光标向后移一