簡介
今天在掘金上看了一篇文章,實現(xiàn)自定義軟鍵盤,發(fā)現(xiàn)其實實現(xiàn)方式比較簡單,不需要改動系統(tǒng)api,只是單純的加載自己的鍵盤布局,隱藏系統(tǒng)彈出的鍵盤,實現(xiàn)數(shù)字錯位,安全輸入軟鍵盤,記錄一下實現(xiàn)過程用于總結
github地址: https://github.com/fushuangdage/CustomView
實現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<Keyboard
xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0px"
android:keyHeight="9%p"
android:keyWidth="25%p"
android:verticalGap="0px"
>
<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="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyHeight="18%p"
android:keyIcon="@drawable/icon_delete_32dp"/>
</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"/>
<Key
android:codes="-4"
android:keyEdgeFlags="right"
android:keyHeight="18%p"
android:keyLabel="確定"
android:keyIcon="@drawable/icon_enter_32dp"/>
</Row>
<Row>
<Key
android:codes="46"
android:keyLabel="."/>
<Key
android:codes="48"
android:keyLabel="0"/>
<Key
android:codes="-3"
android:keyIcon="@drawable/icon_hide_keyboard"/>
</Row>
</Keyboard>
首先編寫關于自定義布局文件.鍵盤布局在xml定義
繼承KeyboardView 畫自己的鍵盤輸入面板,可在ondraw方法中對默認的繪制放置覆蓋繪制
package com.example.admin.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
import java.lang.reflect.Field;
import java.util.List;
/**
* Created by fushuang on 2017/8/22.
*/
public class MykeyBoardView extends KeyboardView {
private Context context;
public MykeyBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public MykeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Keyboard keyboard = getKeyboard();
List<Keyboard.Key> keys=null;
if (keyboard != null) {
keys = keyboard.getKeys();
for (Keyboard.Key key : keys) {
if (key.codes[0]==-4) {
drawKeyBackground(R.drawable.bg_keyboardview_yes,canvas,key);
drawText(canvas,key);
}
}
}
}
private void drawText(Canvas canvas, Keyboard.Key key) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
if (key.label != null) {
String label = key.label.toString();
Field field;
if (label.length() > 1 && key.codes.length < 2) {
int labelTextSize = 0;
try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
labelTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(labelTextSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
} else {
int keyTextSize = 0;
try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
keyTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(keyTextSize);
paint.setTypeface(Typeface.DEFAULT);
}
paint.getTextBounds(key.label.toString(), 0, key.label.toString()
.length(), bounds);
canvas.drawText(key.label.toString(), key.x + (key.width / 2),
(key.y + key.height / 2) + bounds.height() / 2, paint);
} else if (key.icon != null) {
key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2,
key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight());
key.icon.draw(canvas);
}
}
private void drawKeyBackground(int id, Canvas canvas, Keyboard.Key key) {
Drawable drawable = context.getResources().getDrawable(id);
int[] drawableState = key.getCurrentDrawableState();
if (key.codes[0]!=0) {
drawable.setState(drawableState);
}
drawable.setBounds(key.x,key.y, key.x+key.width,key.y+key.height);
drawable.draw(canvas);
}
}
KeyboardUtil 中主要實現(xiàn)當點輸入文本框輸入的隱藏軟鍵盤,顯示自定義鍵盤的邏輯
package com.example.admin.customview;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* Created by fushuang on 2017/8/22.
*/
public class KeyboardUtil implements KeyboardView.OnKeyboardActionListener {
private final MykeyBoardView mKeyBoardView;
private Keyboard mKeyboder;
private EditText mEditText;
private Activity mActivity;
public KeyboardUtil(Activity mActivity) {
this.mActivity = mActivity;
mKeyboder = new Keyboard(mActivity,R.xml.keyboardnumber);
mKeyBoardView = ((MykeyBoardView) mActivity.findViewById(R.id.keyboard_view));
}
public void attachTo(EditText editText){
mEditText = editText;
hideSystemSofeKeyboard(mActivity,editText);
showSoftKeyBoard();
}
private boolean isNumber(String str) {
String wordstr = "0123456789";
return wordstr.contains(str);
}
private void showSoftKeyBoard() {
// mKeyBoardView.setKeyboard(mKeyboder);
List<Keyboard.Key> keys = mKeyboder.getKeys();
List<Keyboard.Key> newkeyList = new ArrayList<Keyboard.Key>();
for (Keyboard.Key key : keys) {
if (key.label!=null && isNumber(key.label.toString())) {
newkeyList.add(key);
}
}
int count = newkeyList.size();
LinkedList<KeyModel> temp=new LinkedList<KeyModel>();
for (int i = 0; i < count; i++) {
temp.add(new KeyModel(48 + i, i + ""));
}
Random random = new Random();
for (int i = 0; i < count; i++) {
int index = random.nextInt(count - i);
KeyModel keyModel = temp.get(index);
newkeyList.get(i).label=keyModel.getLable();
newkeyList.get(i).codes[0]=keyModel.getCode();
temp.remove(index);
}
mKeyBoardView.setKeyboard(mKeyboder);
mKeyBoardView.setEnabled(true);
mKeyBoardView.setPreviewEnabled(false);
mKeyBoardView.setVisibility(View.VISIBLE);
mKeyBoardView.setOnKeyboardActionListener(this);
}
/**
* 隱藏系統(tǒng)鍵盤
*
* @param editText
*/
public static void hideSystemSofeKeyboard(Context context, EditText editText) {
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= 11) {
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(editText, false);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} else {
editText.setInputType(InputType.TYPE_NULL);
}
// 如果軟鍵盤已經(jīng)顯示走净,則隱藏
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = mEditText.getText();
int start = mEditText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 隱藏鍵盤
hideKeyboard();
// if (mOnCancelClick != null) {
// mOnCancelClick.onCancellClick();
// }
} else if (primaryCode == Keyboard.KEYCODE_DONE) {// 隱藏鍵盤
hideKeyboard();
// if (mOnOkClick != null) {
// mOnOkClick.onOkClick();
// }
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
private void hideKeyboard() {
mKeyBoardView.setVisibility(View.GONE);
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
Toast.makeText(mActivity, "swipeLeft", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeRight() {
Toast.makeText(mActivity, "swipeRight", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeDown() {
Toast.makeText(mActivity, "swipeDown", Toast.LENGTH_SHORT).show();
}
@Override
public void swipeUp() {
Toast.makeText(mActivity, "swipeUp", Toast.LENGTH_SHORT).show();
}
}
只是照著網(wǎng)上代碼自己實現(xiàn)了一遍,感覺只是有些方法不太常用.其余的跟自定義控件沒啥區(qū)別