自定义数字软键盘

前言:如下是本人以前实现自定义软键盘的相关代码,分享给大家,希望大家在做这块需求的时候有个方向。

主xml、键盘容器:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
		<!-- 键盘容器 -->
        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/keyboard_bg"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:keyBackground="@drawable/keyboard_bg_selecter"
            android:keyTextColor="@color/key_tx_bg"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>

activity代码部分:

public class MainActivity extends Activity {
	private EditText edit;
	private Context context;
	private Activity activity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		activity = this;
		
		edit = (EditText) findViewById(R.id.edit);
		edit.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int inputback = edit.getInputType();//先保留输入类型
				edit.setInputType(InputType.TYPE_NULL);//防止系统弹出键盘
				edit.setInputType(inputback);//这样设置才会出现光标
				new KeyboardUtil(context, activity, edit).showKeyboard();
				return false;
			}
		});
		
	}
}

处理及定义软键盘工具类:

public class KeyboardUtil {
	private EditText ed;
	private Keyboard numberKeyBoard;//数字键盘
	private KeyboardView keyboardView;
	
	/**
	 * 构造函数
	 * @param ctx
	 * @param act
	 * @param ed
	 */
	public KeyboardUtil(Context ctx, Activity act, EditText ed) {
		this.ed = ed;
		
		//实例化数字键盘
		numberKeyBoard = new Keyboard(ctx, R.xml.symbols);
		//键盘容器
		keyboardView = (KeyboardView)act.findViewById(R.id.keyboard_view);
		keyboardView.setKeyboard(numberKeyBoard);
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(false);//是否上浮信息
		keyboardView.setOnKeyboardActionListener(listener);
	}
	
	//键盘动作监听
	private OnKeyboardActionListener listener = new OnKeyboardActionListener(){
		@Override
		public void onKey(int primaryCode, int[] keyCodes) {
			Editable editable = ed.getText();
			int start = ed.getSelectionStart();
			if(primaryCode == Keyboard.KEYCODE_DELETE){//回退
				if(editable != null && editable.length() > 0){
					editable.delete(start-1, start);
				}
			}else if(primaryCode == 00000){//自定义源代码
			}else if(primaryCode == 11111){//自定义源代码
			}else{//键盘其他键就插入
				editable.insert(start, 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 swipeDown() {
		}
		@Override
		public void swipeLeft() {
		}
		@Override
		public void swipeRight() {
		}
		@Override
		public void swipeUp() {
		}
		
	};
	
	/**
	 * 展示自定义软键盘
	 */
	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);
		}
	}
}

数字键盘的布局实现:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyHeight="30dp" android:keyWidth="33.33%p"
    android:horizontalGap="2px"
    android:verticalGap="2px" >
    <!-- 软键盘一排 -->
    <Row>
    	<Key android:codes="11111" 
    	     android:keyLabel="思埠担保交易安全键盘" 
    	     android:keyHeight="30dp"
    	     android:keyWidth="100%p" />
    </Row>
    <Row android:keyHeight="60dp" android:keyWidth="33.33%p">
    	<Key android:codes="49" android:keyLabel="1" />
    	<Key android:codes="50" android:keyLabel="2" />
    	<Key android:codes="51" android:keyLabel="3" />
    </Row>
    <Row android:keyHeight="60dp" android:keyWidth="33.33%p">
    	<Key android:codes="52" android:keyLabel="4" />
    	<Key android:codes="53" android:keyLabel="5" />
    	<Key android:codes="54" android:keyLabel="6" />
    </Row>
    <Row android:keyHeight="60dp" android:keyWidth="33.33%p">
    	<Key android:codes="55" android:keyLabel="7" />
    	<Key android:codes="56" android:keyLabel="8" />
    	<Key android:codes="57" android:keyLabel="9" />
    </Row>
    <Row android:keyHeight="60dp" android:keyWidth="33.33%p">
    	<Key android:codes="00000" android:keyLabel="" />
    	<Key android:codes="48" android:keyLabel="0" />
    	<Key android:codes="-5" android:keyIcon="@drawable/keyboard_delete_icon" />
    </Row>
</Keyboard>

注意:如果其中部分代码无法提示,需要手动写。。。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值