Android编程实现自定义输入法功能示例【输入密码时防止第三方窃取】
发布时间 - 2026-01-10 22:41:54 点击率:次本文实例讲述了Android编程实现自定义输入法功能。分享给大家供大家参考,具体如下:

对于Android用户而言,一般都会使用第三方的输入法。可是,在输入密码时(尤其是支付相关的密码),使用第三方输入法有极大的安全隐患。目前很多网银类的APP和支付宝等软件在用户输入密码时,都会弹出自定义的输入法而不是直接使用系统输入法。
这里介绍的就是如何实现一个简单的自定义输入法。当然,也可以自己写一个Dialog加上几十个按钮让用户输入,只不过这样显得不够专业。
(一)首先上效果图:
1.前面两个输入框使用了自定义的输入法:
2.第三个输入框没有进行任何设置,因此将使用默认的输入法:
(二)代码简介:
1.主页面布局,由3个输入框加上一个android.inputmethodservice.KeyboardView组成。android.inputmethodservice.KeyboardView是一个系统自带的继承自View的组件,但是它不在android.view这个包下面,因此这里需要写上完整的包名。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--前两个EditText均使用自定义的输入法-->
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="one password"
android:layout_alignParentTop="true"
android:inputType="textPassword" />
<EditText
android:id="@+id/input_password2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input_password"
android:layout_margin="8dp"
android:hint="another password"
android:inputType="textPassword" />
<!--这个EditText使用默认的输入法-->
<EditText
android:id="@+id/input_normal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input_password2"
android:layout_margin="8dp"
android:hint="normal text" />
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
2.KeyboardView是一个显示输入法的容器控件,使用时需要设置具体的输入法面板内容。
(1)首先在res下新建xml目录,然后创建文件keys_layout.xml,即输入法面板的内容。每个row表示一行,Keyboad的属性keyWidth和keyHeight表示每个按键的大小,25%p表示占父组件的25%. Key的属性codes表示该按键的编号(点击时系统回调方法中会返回这个值,用以区分不同的按键),keyLabel表示按键上面显示的文字。还有很多其它的属性,不再陈述。
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="25%p"
android:keyHeight="10%p">
<Row>
<Key
android:codes="55"
android:keyLabel="7"
android:keyEdgeFlags="left" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyLabel="9" />
<!--删除按键长按时连续响应-->
<Key
android:codes="60001"
android:keyLabel="DEL"
android:isRepeatable="true" />
</Row>
<Row>
<Key
android:codes="52"
android:keyLabel="4"
android:keyEdgeFlags="left" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyLabel="6" />
<Key
android:codes="48"
android:keyLabel="0" />
</Row>
<Row>
<Key
android:codes="49"
android:keyLabel="1"
android:keyEdgeFlags="left" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyLabel="3" />
<Key
android:codes="60002"
android:keyLabel="Cancel" />
</Row>
</Keyboard>
(2)为了使用方便,新建一个类:KeyboardBuilder.java,用于初始化自定义输入法和绑定EditText,代码如下:
public class KeyboardBuilder {
private static final String TAG = "KeyboardBuilder";
private Activity mActivity;
private KeyboardView mKeyboardView;
public KeyboardBuilder(Activity ac, KeyboardView keyboardView, int keyBoardXmlResId) {
mActivity = ac;
mKeyboardView = keyboardView;
Keyboard mKeyboard = new Keyboard(mActivity, keyBoardXmlResId);
// Attach the keyboard to the view
mKeyboardView.setKeyboard(mKeyboard);
// Do not show the preview balloons
mKeyboardView.setPreviewEnabled(false);
KeyboardView.OnKeyboardActionListener keyboardListener = new KeyboardView.OnKeyboardActionListener() {
@Override
public void onKey(int primaryCode, int[] keyCodes) {
// Get the EditText and its Editable
View focusCurrent = mActivity.getWindow().getCurrentFocus();
if (focusCurrent == null || !(focusCurrent instanceof EditText)) {
return;
}
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Handle key
if (primaryCode == Constant.CodeCancel) {
hideCustomKeyboard();
} else if (primaryCode == Constant.CodeDelete) {
if (editable != null && start > 0) {
editable.delete(start - 1, start);
}
} else {
// Insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
@Override
public void onPress(int arg0) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeDown() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeUp() {
}
};
mKeyboardView.setOnKeyboardActionListener(keyboardListener);
}
//绑定一个EditText
public void registerEditText(EditText editText) {
// Make the custom keyboard appear
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showCustomKeyboard(v);
} else {
hideCustomKeyboard();
}
}
});
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
showCustomKeyboard(v);
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch");
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
edittext.setSelection(edittext.getText().length());
return true;
}
});
}
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
public void showCustomKeyboard(View v) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if (v != null) {
((InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
}
3.最后是主Activity的代码,这里就很简单了。
/**
* 自定义安全输入法
*/
public class MainActivity extends ActionBarActivity {
private KeyboardBuilder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardview);
builder = new KeyboardBuilder(this, keyboardView, R.xml.keys_layout);
EditText editText = (EditText) findViewById(R.id.input_password);
builder.registerEditText(editText);
EditText editText2 = (EditText) findViewById(R.id.input_password2);
builder.registerEditText(editText2);
}
@Override
public void onBackPressed() {
if (builder != null && builder.isCustomKeyboardVisible()) {
builder.hideCustomKeyboard();
} else {
this.finish();
}
}
}
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android开发动画技巧汇总》、《Android编程之activity操作技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
# Android
# 自定义
# 输入法
# Android文本输入框(EditText)输入密码时显示与隐藏
# Android 实现密码输入框动态明文/密文切换显示效果
# Android程序开发之防止密码输入错误 密码明文显示功能
# Android实现动态显示或隐藏密码输入框的内容
# Android仿支付宝支付密码输入框
# Android 自定义输入支付密码的软键盘实例代码
# Android仿支付宝、京东的密码键盘和输入框
# Android自定义View仿支付宝输入六位密码功能
# Android的支付密码输入框实现浅析
# Android高仿微信支付密码输入控件
# Android仿微信/支付宝密码输入框
# Android编程实现打勾显示输入密码功能
# 是一个
# 输入框
# 第三方
# 绑定
# 输入密码
# 进阶
# 操作技巧
# 相关内容
# 尤其是
# 感兴趣
# 给大家
# 很简单
# 弹出
# 第三个
# 更多关于
# 还有很多
# 所述
# 写上
# 几十个
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel怎么清理缓存_Laravel optimize clear命令详解
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
网易LOFTER官网链接 老福特网页版登录地址
,在苏州找工作,上哪个网站比较好?
JavaScript如何实现错误处理_try...catch如何捕获异常?
如何用虚拟主机快速搭建网站?详细步骤解析
进行网站优化必须要坚持的四大原则
Laravel如何创建自定义中间件?(Middleware代码示例)
Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】
如何在阿里云部署织梦网站?
如何打造高效商业网站?建站目的决定转化率
如何基于PHP生成高效IDC网络公司建站源码?
齐河建站公司:营销型网站建设与SEO优化双核驱动策略
Laravel怎么进行浏览器测试_Laravel Dusk自动化浏览器测试入门
微信推文制作网站有哪些,怎么做微信推文,急?
如何在Windows虚拟主机上快速搭建网站?
Laravel中DTO是什么概念_在Laravel项目中使用数据传输对象(DTO)
详解CentOS6.5 安装 MySQL5.1.71的方法
Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】
Laravel如何实现API资源集合?(Resource Collection教程)
如何用花生壳三步快速搭建专属网站?
如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南
利用JavaScript实现拖拽改变元素大小
Laravel如何配置和使用缓存?(Redis代码示例)
Android使用GridView实现日历的简单功能
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
Claude怎样写结构化提示词_Claude结构化提示词写法【教程】
如何用腾讯建站主机快速创建免费网站?
如何用PHP快速搭建CMS系统?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
JavaScript如何实现继承_有哪些常用方法
Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
Linux系统命令中screen命令详解
Laravel如何使用Passport实现OAuth2?(完整配置步骤)
如何在自有机房高效搭建专业网站?
千问怎样用提示词获取健康建议_千问健康类提示词注意事项【指南】
,交易猫的商品怎么发布到网站上去?
如何快速搭建高效服务器建站系统?
如何快速生成凡客建站的专业级图册?
Laravel如何自定义错误页面(404, 500)?(代码示例)
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
无锡营销型网站制作公司,无锡网选车牌流程?
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
浅谈redis在项目中的应用
Laravel怎么发送邮件_Laravel Mail类SMTP配置教程
EditPlus中的正则表达式 实战(1)
python中快速进行多个字符替换的方法小结
HTML5空格在Angular项目里怎么处理_Angular中空格的渲染问题【详解】
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口

