android自定义键盘
首先来了解几个类
android.inputmethodservice.KeyboardView 主要管理键盘的显示相关的特性,如键盘的整体背景颜色,每一个键的背景颜色,每一个键的自题的颜色大小等待。以及各种时间的触发和处理。
android.inputmethodservice.Keyboard 就像他的名字,这个类描述的是一个键盘,包括了每个键和值的对应关系,以及每个键的大小和具体位置,在初始化这个类的时候,我们需要将一个键盘布局文xml件传给此类的方法,此xml文件中定义了各个键和值的对应关系,以及所有键的布局关系。
下面我们首先来定义我们Keyboard的布局文件,此文件应该放在res/xml中
character_symbols.xml(英文字母键盘)
<?xml version="1.0" encoding="UTF-8"?> <Keyboard android:keyWidth="10.000002%p" android:keyHeight="50dp" android:horizontalGap="1.0px" android:verticalGap="1.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="4445" android:keyLabel="123#" /> <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="-4" android:keyEdgeFlags="right" android:keyLabel="完成" /> </Row> </Keyboard>
number_symbols.xml(数字键盘)
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:horizontalGap="1sp" android:keyHeight="10%p" android:keyWidth="33.33%" android:verticalGap="1sp"> <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="4444" android:keyLabel="abc" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" /> </Row> </Keyboard>
相关属性
android:keyWidth="10.000002%p" 表示每一个键的宽度
android:keyHeight="50dp" 表示每一个键的高度
android:horizontalGap="1.0px"表示行间距
android:verticalGap="1.0px"表示列间距
<Key> 表示一个键
<Row> 行 没一行可以有若干个<Key>
codes 表示每一个键的键 系统已经定义好了很多,我们可以直接用,当然我们也可一自定义。
keyIcon 表示每一个键的文字
keyLabel 表示每一个键的显示的图标
keyIcon keyLabel 两者只能选一个
接下来我们需要定义一个工具类来管理键盘的各种功能
package com.liaoli.keyboard; import android.app.Activity; import android.content.Context; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; import android.text.InputType; import android.view.View; import android.widget.EditText; import org.w3c.dom.Text; import java.util.List; /** * Created by liaoli on 15-10-16. */ public class KeyboardUtil implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; //数字键盘 private Keyboard k_number; //英文键盘 private Keyboard k_character; private EditText et; private boolean isUpperCase = false; public KeyboardUtil(Activity activity, Context context, EditText et) { this.k_number = new Keyboard(context, R.xml.number_symbols); this.k_character = new Keyboard(context, R.xml.character_symbols); this.et = et; keyboardView = (KeyboardView) activity.findViewById(R.id.keyvoard_view); showKeyboard(k_character); } /** * 此方法主要在切换输入法时用到 * * @param keyboard */ private void showKeyboard(Keyboard keyboard) { keyboardView.setKeyboard(keyboard); keyboardView.setPreviewEnabled(false); keyboardView.setOnKeyboardActionListener(this); keyboardView.setEnabled(true); keyboardView.setVisibility(View.VISIBLE); } @Override public void onPress(int primaryCode) { } @Override public void onRelease(int primaryCode) { } @Override public void onKey(int primaryCode, int[] keyCodes) { //在此根据每一个键的primaryCode来处理相应的逻辑,主要是处理自定义的primary处理逻辑 Editable editable = et.getText(); int start = et.getSelectionStart(); if (primaryCode == Keyboard.KEYCODE_DELETE) { if (editable != null && editable.length() > 0) { if (start > 0) { editable.delete(start - 1, start); } } } else if (primaryCode == 4444) { //数字键盘切换到英文字符键盘 showKeyboard(k_character); } else if (primaryCode == 4445) { //英文键盘切换到数字键盘 showKeyboard(k_number); } else if (primaryCode == Keyboard.KEYCODE_DONE) { hideKeyboard(); } else if (primaryCode == Keyboard.KEYCODE_SHIFT) { //大小写切换 changKey(); showKeyboard(k_character); } else { //将输入的内容添加到输入框中 editable.insert(start, Character.toString((char) primaryCode)); } } private void changKey() { List<Keyboard.Key> keyList = k_character.getKeys(); if (isUpperCase) { for (int i = 0; i < keyList.size(); i++) { Keyboard.Key key = keyList.get(i); if (key.label != null && isLetter(key.label.toString())) { key.label = key.label.toString().toLowerCase(); key.codes[0] += 32; } } isUpperCase = !isUpperCase; } else { for (int i = 0; i < keyList.size(); i++) { Keyboard.Key key = keyList.get(i); if (key.label != null && isLetter(key.label.toString())) { key.label = key.label.toString().toUpperCase(); key.codes[0] -= 32; } } isUpperCase = !isUpperCase; } } private boolean isLetter(String s) { String letters = "abcdefghijklmnopqrstuvwxyz"; int index = letters.indexOf(s.toLowerCase()); return index > -1; } @Override public void onText(CharSequence text) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } public void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); } } public void hideKeyboard() { keyboardView.setVisibility(View.INVISIBLE); } }
接下来我们就可通过这个工具类来使用我们的自定义键盘了。
package com.liaoli.keyboard; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.InputType; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private EditText userName; private EditText passward; private KeyboardUtil keyboardUtil; // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userName = (EditText) findViewById(R.id.user_name); passward = (EditText) findViewById(R.id.password); userName.setOnTouchListener(this); passward.setOnTouchListener(this); } public void register(View view){ String userName = this.userName.getText().toString().trim(); String password = this.passward.getText().toString().trim(); if(TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)){ Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"恭喜你!"+userName + "已注册成功!",Toast.LENGTH_SHORT).show(); if (keyboardUtil != null) { keyboardUtil.hideKeyboard(); } } } @Override public boolean onTouch(View v, MotionEvent event) { EditText et = null; if(v instanceof EditText){ et = (EditText) v; //不让系统输入法弹出 int inputMoudle = et.getInputType(); et.setInputType(InputType.TYPE_NULL); keyboardUtil = new KeyboardUtil(this,this,et); keyboardUtil.showKeyboard(); et.extendSelection(0); et.setInputType(inputMoudle); //必须向上传递onTouch事件,光标显示 return false; } return true; } }
activity_main.xml wenjian
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/edit_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <EditText android:id="@+id/user_name" android:layout_width="fill_parent" android:layout_height="50dp" android:hint="用户名" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="50dp" android:password="true" android:hint="密码" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:onClick="register" android:layout_below="@+id/edit_layout" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="27dp" android:layout_marginEnd="27dp" /> <android.inputmethodservice.KeyboardView android:id="@+id/keyvoard_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:keyTextColor="#FFFACD" android:keyBackground="@drawable/key_selector" android:layout_alignParentBottom="true"/> </RelativeLayout>
效果