android自定义金额输入键盘_Android自定义软键盘的实现
先看界面布局文件
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
android:id="@+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@color/lightblack"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyTextColor="@color/white"
android:visibility="gone" />
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
android:id="@+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@color/lightblack"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyTextColor="@color/white"
android:visibility="gone" />
通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下
qwerty.xml内容
android:horizontalGap="0.0px" android:verticalGap="0.0px"
xmlns:android="http://schemas.android.com/apk/res/android">
android:keyLabel="q" />
android:keyLabel="p" />
android:keyEdgeFlags="left" android:keyLabel="a" />
android:keyLabel="l" />
android:keyEdgeFlags="left" android:isModifier="true"
android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
android:keyLabel="12#" />
android:keyLabel="," />
android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
android:keyLabel="." />
android:keyEdgeFlags="right" android:keyLabel="完成" />
android:horizontalGap="0.0px" android:verticalGap="0.0px"
xmlns:android="http://schemas.android.com/apk/res/android">
android:keyLabel="q" />
android:keyLabel="p" />
android:keyEdgeFlags="left" android:keyLabel="a" />
android:keyLabel="l" />
android:keyEdgeFlags="left" android:isModifier="true"
android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
android:keyLabel="12#" />
android:keyLabel="," />
android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
android:keyLabel="." />
android:keyEdgeFlags="right" android:keyLabel="完成" />
symbols.xml 内容
android:keyWidth="25%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="@dimen/key_height">
android:keyIcon="@drawable/sym_keyboard_left" />
android:keyIcon="@drawable/sym_keyboard_right" />
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyLabel="完成" />
android:keyWidth="25%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="@dimen/key_height">
android:keyIcon="@drawable/sym_keyboard_left" />
android:keyIcon="@drawable/sym_keyboard_right" />
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyLabel="完成" />
KeydemoActivity.java
package cn.key;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
public class KeydemoActivity extends Activity {
private Context ctx;
private Activity act;
private EditText edit;
private EditText edit1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
act = this;
edit = (EditText) this.findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
edit1 = (EditText) this.findViewById(R.id.edit1);
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new KeyboardUtil(act, ctx, edit).showKeyboard();
return false;
}
});
edit1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int inputback = edit1.getInputType();
edit1.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(act, ctx, edit1).showKeyboard();
edit1.setInputType(inputback);
return false;
}
});
}
}
package cn.key;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
public class KeydemoActivity extends Activity {
private Context ctx;
private Activity act;
private EditText edit;
private EditText edit1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
act = this;
edit = (EditText) this.findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
edit1 = (EditText) this.findViewById(R.id.edit1);
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new KeyboardUtil(act, ctx, edit).showKeyboard();
return false;
}
});
edit1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int inputback = edit1.getInputType();
edit1.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(act, ctx, edit1).showKeyboard();
edit1.setInputType(inputback);
return false;
}
});
}
}
KeyboardUtil.java
package cn.key;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class KeyboardUtil {
private Context ctx;
private Activity act;
private KeyboardView keyboardView;
private Keyboard k1;// 字母键盘
private Keyboard k2;// 数字键盘
public boolean isnun = false;// 是否数据键盘
public boolean isupper = false;// 是否大写
private EditText ed;
public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.act = act;
this.ctx = ctx;
this.ed = edit;
k1 = new Keyboard(ctx, R.xml.qwerty);
k2 = new Keyboard(ctx, R.xml.symbols);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
}
private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(k1);
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
if (isnun) {
isnun = false;
keyboardView.setKeyboard(k1);
} else {
isnun = true;
keyboardView.setKeyboard(k2);
}
} else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // go right
if (start
ed.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};
/**
* 键盘大小写切换
*/
private void changeKey() {
List keylist = k1.getKeys();
if (isupper) {//大写切换小写
isupper = false;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0]+32;
}
}
} else {//小写切换大写
isupper = true;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0]-32;
}
}
}
}
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
}
package cn.key;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class KeyboardUtil {
private Context ctx;
private Activity act;
private KeyboardView keyboardView;
private Keyboard k1;// 字母键盘
private Keyboard k2;// 数字键盘
public boolean isnun = false;// 是否数据键盘
public boolean isupper = false;// 是否大写
private EditText ed;
public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.act = act;
this.ctx = ctx;
this.ed = edit;
k1 = new Keyboard(ctx, R.xml.qwerty);
k2 = new Keyboard(ctx, R.xml.symbols);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
}
private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(k1);
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
if (isnun) {
isnun = false;
keyboardView.setKeyboard(k1);
} else {
isnun = true;
keyboardView.setKeyboard(k2);
}
} else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // go right
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};
/**
* 键盘大小写切换
*/
private void changeKey() {
List keylist = k1.getKeys();
if (isupper) {//大写切换小写
isupper = false;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0]+32;
}
}
} else {//小写切换大写
isupper = true;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0]-32;
}
}
}
}
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
}
源码下载地址:http://download.csdn.net/detail/hfsu0419/4534209
android自定义金额输入键盘_Android自定义软键盘的实现相关推荐
- android自定义金额输入键盘_Android 自定义输入支付密码的软键盘实例代码
Android 自定义输入支付密码的软键盘 有项目需求需要做一个密码锁功能,还有自己的软键盘,类似与支付宝那种,这里是整理的资料,大家可以看下,如有错误,欢迎留言指正 需求:要实现类似支付宝的输入支付 ...
- android自定义金额输入键盘_Android 自定义控件 - 仿支付宝数字键盘
原标题:Android 自定义控件 - 仿支付宝数字键盘 简介 在一些带有支付功能的 App 中,输入的密码一般只能是纯数字,虽然我们可以指定 EditText 输入框只能输入数字,但是为了提供用户的 ...
- android 调出键盘表情_android 表情,软键盘冲突解决方案(仿微博等SNS应用)
之前总想搞一下这个模块,可是由于忙碌总是推迟,现在就把这块好好的弥补过来,下面是我实现的思路.本人才疏学浅,还望大家不要见笑. 首先我们还是先看写示例: 上面应用应该不用我过多介绍,下面我简单介绍下我 ...
- Android爬坑之旅:软键盘挡住输入框问题的终极解决方式
本文由BarryZhang原创,同一时候首发于diycode.cc.barryzhang.com .github.com/barryhappy.非商业转载请注明作者和原文链接. 前言 开发做得久了.总 ...
- Android爬坑之旅:软键盘挡住输入框问题的终极解决方案
前言 开发做得久了,总免不了会遇到各种坑. 而在Android开发的路上,『软键盘挡住了输入框』这个坑,可谓是一个旷日持久的巨坑--来来来,我们慢慢看. 入门篇 Base 最基本的情况,如图所示:在页 ...
- 【Android笔记50】Android应用如何获取系统服务(软键盘管理器、闹钟管理器)
这篇文章,主要介绍Android应用如何获取系统服务(软键盘管理器.闹钟管理器). 目录 一.获取系统服务 1.1.软键盘显示和隐藏 (1)showSoftInput显示软键盘
- android隐藏软键盘方法,Android使用InputMethodManager显示和隐藏软键盘
Android主要用InputMethodManager来对软键盘进行管理.手动显示或隐藏软键盘前需要先获取InputMethodManager. InputMethodManager imm = ( ...
- android EditText取消焦点且退出软键盘
android EditText取消焦点且退出软键盘 最近发现,如果在一个activity中直接加入EditText的话,每次打开这个activity的时候,就会自动打开软键盘,并且点哪里都取消不了. ...
- android自定义金额输入键盘_触摸键盘的设计解析
本文为PMCAFF作者 呆呆丶于社区发布 众所周知,移动设备的触摸键盘相比于电脑的实体键盘而言,易用性较差.原因有很多,比如使用习惯,人机工程原理,触摸实感差异等等.这些原因导致我们在使用移动设备的触 ...
最新文章
- 基于RFID的防伪系统设计
- Exchange2010SP1配置OWA下次登录时更改密码提醒
- html+引导,html – 引导点的CSS样式
- apache isis_使用Apache Isis快速进行SEMAT应用程序开发
- Spark入门:也可以用Java创建轻量级的RESTful应用程序
- python文件读取写入实践_python文件写入实例分析
- 上海三校生应用计算机考试试卷,三校生计算机考试试题.doc
- java 链表逆转_java 实现单链表逆转详解及实例代码
- java商城项目_javaweb实战之商城项目开发(一)
- python多维数据聚类可视化_基于python3的可视化数据聚类系统(k-means算法和k-中心点算法)...
- 利用Java实现简单的日报管理
- Java怎么连接数据库 (使用技术及步骤原理,入门即会)
- Python抠图程序源码
- 京东联盟API-二合一链接转链接口-线报转链-京粉转链接口
- JavaScript判断数组的方法
- JAVA实现IDcard身份证自动识别
- excel的IRR函数
- android jpeg图片变圆,Android 将一张图片变为圆形图片
- 技术人生:高山仰止,景行观止,虽不能至,我心向往之
- flac转mp3教程flac怎么转mp3才能减小音质的丢失