/**
* 输入数字密码的键盘布局控件。
*/
public class PasswordKeyboardView extends KeyboardView implements
android.inputmethodservice.KeyboardView.OnKeyboardActionListener {
// 用于区分左下角空白的按键
private static final int KEYCODE_EMPTY = -10;
private int mDeleteBackgroundColor;
private Rect mDeleteDrawRect;
private Drawable mDeleteDrawable;
private IOnKeyboardListener mOnKeyboardListener;
public PasswordKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public PasswordKeyboardView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs,
int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PasswordKeyboardView, defStyleAttr, 0);
mDeleteDrawable = a.getDrawable(
R.styleable.PasswordKeyboardView_pkvDeleteDrawable);
mDeleteBackgroundColor = a.getColor(
R.styleable.PasswordKeyboardView_pkvDeleteBackgroundColor,
Color.TRANSPARENT);
a.recycle();
// 设置软键盘按键的布局
Keyboard keyboard = new Keyboard(context,
R.xml.keyboard_number_password);
setKeyboard(keyboard);
setEnabled(true);
setPreviewEnabled(false);
setOnKeyboardActionListener(this);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 遍历所有的按键
List<Keyboard.Key> keys = getKeyboard().getKeys();
for (Keyboard.Key key : keys) {
// 如果是左下角空白的按键,重画按键的背景
if (key.codes[0] == KEYCODE_EMPTY) {
drawKeyBackground(key, canvas, mDeleteBackgroundColor);
}
// 如果是右下角的删除按键,重画背景,并且绘制删除的图标
else if (key.codes[0] == Keyboard.KEYCODE_DELETE) {
drawKeyBackground(key, canvas, mDeleteBackgroundColor);
drawDeleteButton(key, canvas);
}
}
}
// 绘制按键的背景
private void drawKeyBackground(Keyboard.Key key, Canvas canvas,
int color) {
ColorDrawable drawable = new ColorDrawable(color);
drawable.setBounds(key.x, key.y,
key.x + key.width, key.y + key.height);
drawable.draw(canvas);
}
// 绘制删除按键
private void drawDeleteButton(Keyboard.Key key, Canvas canvas) {
if (mDeleteDrawable == null)
return;
// 计算删除图标绘制的坐标
if (mDeleteDrawRect == null || mDeleteDrawRect.isEmpty()) {
int intrinsicWidth = mDeleteDrawable.getIntrinsicWidth();
int intrinsicHeight = mDeleteDrawable.getIntrinsicHeight();
int drawWidth = intrinsicWidth;
int drawHeight = intrinsicHeight;
// 限制图标的大小,防止图标超出按键
if (drawWidth > key.width) {
drawWidth = key.width;
drawHeight = drawWidth * intrinsicHeight / intrinsicWidth;
}
if (drawHeight > key.height) {
drawHeight = key.height;
drawWidth = drawHeight * intrinsicWidth / intrinsicHeight;
}
// 获取删除图标绘制的坐标
int left = key.x + (key.width - drawWidth) / 2;
int top = key.y + (key.height - drawHeight) / 2;
mDeleteDrawRect = new Rect(left, top,
left + drawWidth, top + drawHeight);
}
// 绘制删除的图标
if (mDeleteDrawRect != null && !mDeleteDrawRect.isEmpty()) {
mDeleteDrawable.setBounds(mDeleteDrawRect.left,
mDeleteDrawRect.top, mDeleteDrawRect.right,
mDeleteDrawRect.bottom);
mDeleteDrawable.draw(canvas);
}
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
// 处理按键的点击事件
// 点击删除按键
if (primaryCode == Keyboard.KEYCODE_DELETE) {
if (mOnKeyboardListener != null) {
mOnKeyboardListener.onDeleteKeyEvent();
}
}
// 点击了非左下角按键的其他按键
else if (primaryCode != KEYCODE_EMPTY) {
if (mOnKeyboardListener != null) {
mOnKeyboardListener.onInsertKeyEvent(
Character.toString((char) primaryCode));
}
}
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
/**
* 设置键盘的监听事件。
*
* @param listener
* 监听事件
*/
public void setIOnKeyboardListener(IOnKeyboardListener listener) {
this.mOnKeyboardListener = listener;
}
public interface IOnKeyboardListener {
void onInsertKeyEvent(String text);
void onDeleteKeyEvent();
}
}
<declare-styleable name="PasswordKeyboardView"> <attr name="pkvDeleteDrawable" format="reference"/> <attr name="pkvDeleteBackgroundColor" format="color|reference"/> </declare-styleable>
<?xml version="1.0" encoding="utf-8"?>
<Keyboard
xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33.33333%p"
android:keyHeight="8%p"
android:horizontalGap="1dp"
android:verticalGap="1dp">
<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="-10"
android:keyLabel=""/>
<Key
android:codes="48"
android:keyLabel="0"/>
<Key
android:codes="-5"
android:keyIcon="@mipmap/keyboard_backspace"/>
</Row>
</Keyboard>
<[包名].PasswordKeyboardView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#b0b0b0" android:focusable="true" android:focusableInTouchMode="true" android:keyBackground="#ffffff" android:keyTextColor="#000000" android:shadowColor="#00000000" android:shadowRadius="0" app:pkvDeleteBackgroundColor="#d2d2d2" app:pkvDeleteDrawable="@drawable/keyboard_backspace" />
// 0-9 的数字
private final List<Character> keyCodes = Arrays.asList(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
/**
* 随机打乱数字键盘上显示的数字顺序。
*/
public void shuffleKeyboard() {
Keyboard keyboard = getKeyboard();
if (keyboard != null && keyboard.getKeys() != null
&& keyboard.getKeys().size() > 0) {
// 随机排序数字
Collections.shuffle(keyCodes);
// 遍历所有的按键
List<Keyboard.Key> keys = getKeyboard().getKeys();
int index = 0;
for (Keyboard.Key key : keys) {
// 如果按键是数字
if (key.codes[0] != KEYCODE_EMPTY
&& key.codes[0] != Keyboard.KEYCODE_DELETE) {
char code = keyCodes.get(index++);
key.codes[0] = code;
key.label = Character.toString(code);
}
}
// 更新键盘
setKeyboard(keyboard);
}
}
<[包名].PasswordKeyboardView android:shadowColor="@color/transparent" android:shadowRadius="0" ... />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有