Design and implementation code _android of Android custom soft keyboard

Source: Internet
Author: User
Tags lowercase visibility


Accidentally found the Android.inputmethodservice.Keyboard class, that is, Android can customize the keyboard class, made a simple example for everyone to refer to.



The effect is as follows:






First look at the interface layout file


<?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> 


You can see from the layout file that there are two input boxes on the interface, one of which is a password input box and a hidden keyboard control on the interface.



Create a new XML folder under RES and create a new qwerty.xml and Symbols.xml file in the XML folder. Qwerty.xml is the letter keyboard layout, Symbols.xml is the numeric keypad layout, as follows:



Qwerty.xml Content


<?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 Content


<?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;//Letter Keyboard private keyboard k2;//numeric keypad public Boolean Isnun = false;//whether the data keyboard is public Boole 
  
    An isupper = false;//is capitalized private edittext ed; 
        Public Keyboardutil (the Activity Act, the 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 Onreleas 
  
        E (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) {//complete hidekeyboard (); else if (Primarycode = = Keyboard.keycode_delete) {//fallback if (editable!= null && editable.length 
                    () > 0) {if (Start > 0) {editable.delete (start-1, start); 
                }} else if (Primarycode = keyboard.keycode_shift) {//Case switching 
                ChangeKey (); 
  
            Keyboardview.setkeyboard (K1); else if (Primarycode = = Keyboard.keycode_mode_change) {//numeric keypad switch if (Isnun) {Isnu 
                    n = 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)); 
      
    } 
        } 
    }; 
        /** * Keyboard case switching/private void ChangeKey () {list<key> keylist = K1.getkeys (); 
            if (isupper) {//uppercase toggle Lowercase 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 {//lowercase toggle Uppercase 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; 

 } 
  
}


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.