开始养成良好的写博客习惯...能帮到别人就更好了~
=================================================
最近用到一个自定义的数字键盘,做个记录以便以后翻阅~
先上图:
很简单的功能,方便用户快速编辑想要输入的数字~
先上布局的~main.xml 一个输入框一个自定义键盘~
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 自定义的数字键盘 -->
<android.inputmethodservice.KeyboardView
android:id="@+id/kerboard_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/lightblack"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyTextColor="@color/white" />
<!-- 信息输入框 -->
<EditText
android:id="@+id/et_money_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="@id/kerboard_num"
android:text="@string/money"/>
</RelativeLayout>
按下动作 btn_keyboard_key.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/normal_key_hl_bg" />
<item android:drawable="@drawable/normal_key_bg" />
</selector>
继续自定义键盘的布局文件 ~res/xml/number_input.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="25%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="@dimen/num_height">
<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:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_delete" />
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
<Key android:codes="46" android:keyLabel="."
android:keyEdgeFlags="right" />
</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="-3" android:keyHeight="100dip"
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyLabel="完成" />
</Row>
<Row>
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="5656" android:keyLabel="00" />
<Key android:codes="5757" android:keyLabel="000" />
</Row>
</Keyboard>
宽高按需求来设~
android:codes 是标识,在JAVA代码中可以定义其输出文本
android:keyLabel 显示的文本内容
自定义一个键盘工具类 NumberKerboardUtil.java
package com.ob.main;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.widget.EditText;
import android.widget.TextView;
public class NumberKerboardUtil {
private TextView tv_money_text;
private Keyboard kb;//数字键盘View
private KeyboardView kerboard_num;
public NumberKerboardUtil(Activity activity,Context context, EditText tv_money_text){
this.tv_money_text = tv_money_text;
kb = new Keyboard(activity, R.xml.number_input);
kerboard_num = (KeyboardView)activity.findViewById(R.id.kerboard_num);
kerboard_num.setKeyboard(kb);
kerboard_num.setEnabled(true);
kerboard_num.setPreviewEnabled(false);//预览功能
kerboard_num.setOnKeyboardActionListener(listenner);
}
private OnKeyboardActionListener listenner = new OnKeyboardActionListener() {
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = (Editable) tv_money_text.getText();
int start = tv_money_text.getSelectionStart();
if(primaryCode == Keyboard.KEYCODE_DELETE){//DEL按键
if(editable !=null && editable.length()>0){
if(start>0){
editable.delete(start-1, start);
}
}
}else if(primaryCode == Keyboard.KEYCODE_CANCEL){//KEYCODE_CANCEL = -3 完成操作
}else if(primaryCode == 5656){//00 按键
editable.append("00");
}else if(primaryCode == 5757){//000 按键
editable.append("000");
}
else {//其它按键 ASCII码表转换
editable.insert(start, Character.toString((char) primaryCode));
}
}
public void swipeUp() {
// TODO Auto-generated method stub
}
public void swipeRight() {
// TODO Auto-generated method stub
}
public void swipeLeft() {
// TODO Auto-generated method stub
}
public void swipeDown() {
// TODO Auto-generated method stub
}
public void onText(CharSequence text) {
// TODO Auto-generated method stub
}
public void onRelease(int primaryCode) {
// TODO Auto-generated method stub
}
public void onPress(int primaryCode) {
// TODO Auto-generated method stub
}
};
}
关键地方有注释~primaryCode即设置的codes标识,
最后MainActivity里了~
package com.ob.main;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText et_money_text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_money_text = (EditText) findViewById(R.id.et_money_text);
et_money_text.setInputType(InputType.TYPE_NULL);//强制隐藏EditText的系统键盘
new NumberKerboardUtil(this, getApplication(), et_money_text);
}
}
要有英文的就创建两个xml,网上实例蛮多~勉...
总体回顾一遍~感觉萌萌哒...
藏个 InputMethodService的生命周期~
键盘和自定义输入框的下载地址:http://download.csdn.net/detail/pds574834424/8141937