欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义软键盘的设计与实现代码

程序员文章站 2024-03-01 12:00:10
偶然间发现了android.inputmethodservice.keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考。 效果如下:...

偶然间发现了android.inputmethodservice.keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考。

效果如下:
Android自定义软键盘的设计与实现代码

Android自定义软键盘的设计与实现代码

先看界面布局文件

 <?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:visibility="gone" /> 
  </relativelayout> 
  
</linearlayout> 

通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。

在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下:

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> 

keydemoactivity.java

package cn.key; 
  
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.java

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。