Android 自定义数字键盘实现教程
在 Android 应用中,系统默认的键盘可能无法满足特定需求(如仅支持数字输入、自定义布局等)。本文将详细介绍如何实现一个自定义数字键盘,并提供完整的代码示例。
实现步骤
1. 创建自定义键盘布局
首先,我们需要定义一个自定义键盘的布局文件。在 res/xml 目录下创建一个 XML 文件(例如 number_keyboard.xml),用于描述键盘的按键布局。
文件:res/xml/number_keyboard.xml
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p"
android:keyHeight="60dp"
android:horizontalGap="5dp"
android:verticalGap="5dp">
<!-- 第一行:1, 2, 3 -->
<Row>
<Key android:codes="49" android:keyLabel="1" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
</Row>
<!-- 第二行:4, 5, 6 -->
<Row>
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
</Row>
<!-- 第三行:7, 8, 9 -->
<Row>
<Key android:codes="55" android:keyLabel="7" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
</Row>
<!-- 第四行:删除键, 0, 完成键 -->
<Row>
<Key android:codes="-5" android:keyLabel="删除" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="-1" android:keyLabel="完成" />
</Row>
</Keyboard>
参数说明:
android:keyWidth:按键的宽度(20%p 表示占父容器宽度的 20%)。
android:keyHeight:按键的高度。
android:codes:按键的键值(如 49 对应字符 ‘1’)。
android:keyLabel:按键上显示的文本。
2. 在布局文件中添加 KeyboardView
接下来,在 Activity 的布局文件中添加一个 KeyboardView,用于显示自定义键盘。
文件:res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- 输入框 -->
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="请输入数字" />
<!-- 自定义键盘 -->
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>
3. 在 Activity 中实现键盘逻辑
在 Activity 中加载自定义键盘,并处理按键事件。
文件:MainActivity.java
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private KeyboardView keyboardView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化视图
editText = findViewById(R.id.editText);
keyboardView = findViewById(R.id.keyboardView);
// 加载自定义键盘布局
Keyboard keyboard = new Keyboard(this, R.xml.number_keyboard);
keyboardView.setKeyboard(keyboard);
// 设置键盘事件监听器
keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
@Override
public void onKey(int primaryCode, int[] keyCodes) {
// 处理按键事件
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE: // 删除键
String currentText = editText.getText().toString();
if (currentText.length() > 0) {
editText.setText(currentText.substring(0, currentText.length() - 1));
}
break;
case Keyboard.KEYCODE_DONE: // 完成键
finish(); // 关闭当前 Activity
break;
default: // 数字键
char code = (char) primaryCode;
editText.append(String.valueOf(code));
break;
}
}
@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() {
// 处理上滑(可选)
}
});
}
}
4. 运行效果
运行应用后,你会看到一个自定义的数字键盘:
点击数字键,输入框会显示对应的数字。
点击删除键,会删除最后一个字符。
点击完成键,会关闭当前 Activity。
5. 关键点解析
KeyboardView:
用于显示自定义键盘。
必须使用完整的包名 android.inputmethodservice.KeyboardView。
android:codes:
定义按键的键值。
支持自定义值(如 -5 表示删除键,-1 表示完成键)。
OnKeyboardActionListener:
监听键盘事件。
主要实现 onKey 方法,处理按键逻辑。