自定义数字键盘(固定button键盘)

最近做支付相关软件,老板不想用系统的键盘,要自己写一个固定的按钮做键盘,感觉一个一个按钮加上去 挺麻烦的,而且有好几个页面都要使用这个键盘,做为程序员,我来封装一下。话不多说,直接上核心代码,主要是一个fragment和一个layout

1:MyKeyBoardFragment:原理就是点击按钮后模拟键盘点击事件,封装在performKeyDown方法中。

package com.dingmore.terminal.ui.more;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.dingmore.terminal.R;

public class MyKeyBoardFragment extends Fragment implements OnClickListener{
	Activity mActivity;
	View rootView;

	private Button button_1,button_2,button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_0,button_del,button_point;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mActivity=getActivity();
		rootView=inflater.inflate(R.layout.keyboard_layout, container, false);
		initView();
		return rootView;
	}
	
	private void initView() {
		button_0=(Button)rootView.findViewById(R.id.button_0);
		button_1=(Button)rootView.findViewById(R.id.button_1);
		button_2=(Button)rootView.findViewById(R.id.button_2);
		button_3=(Button)rootView.findViewById(R.id.button_3);
		button_4=(Button)rootView.findViewById(R.id.button_4);
		button_5=(Button)rootView.findViewById(R.id.button_5);
		button_6=(Button)rootView.findViewById(R.id.button_6);
		button_7=(Button)rootView.findViewById(R.id.button_7);
		button_8=(Button)rootView.findViewById(R.id.button_8);
		button_9=(Button)rootView.findViewById(R.id.button_9);
		button_point=(Button)rootView.findViewById(R.id.button_point);
		button_del=(Button)rootView.findViewById(R.id.button_del);
		button_0.setOnClickListener(this);
		button_1.setOnClickListener(this);
		button_2.setOnClickListener(this);
		button_3.setOnClickListener(this);
		button_4.setOnClickListener(this);
		button_5.setOnClickListener(this);
		button_6.setOnClickListener(this);
		button_7.setOnClickListener(this);
		button_8.setOnClickListener(this);
		button_9.setOnClickListener(this);
		button_point.setOnClickListener(this);
		button_del.setOnClickListener(this);
		button_del.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				performKeyDown(KeyEvent.KEYCODE_CLEAR);
				return false;
			}
		});
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.button_0:
			performKeyDown(KeyEvent.KEYCODE_0);
			break;
		case R.id.button_1:
			performKeyDown(KeyEvent.KEYCODE_1);
			break;
		case R.id.button_2:
			performKeyDown(KeyEvent.KEYCODE_2);
			break;
		case R.id.button_3:
			performKeyDown(KeyEvent.KEYCODE_3);
			break;
		case R.id.button_4:
			performKeyDown(KeyEvent.KEYCODE_4);
			break;
		case R.id.button_5:
			performKeyDown(KeyEvent.KEYCODE_5);
			break;
		case R.id.button_6:
			performKeyDown(KeyEvent.KEYCODE_6);
			break;
		case R.id.button_7:
			performKeyDown(KeyEvent.KEYCODE_7);
			break;
		case R.id.button_8:
			performKeyDown(KeyEvent.KEYCODE_8);
			break;
		case R.id.button_9:
			performKeyDown(KeyEvent.KEYCODE_9);
			break;
		case R.id.button_point:
			performKeyDown(KeyEvent.KEYCODE_NUMPAD_DOT);
			break;
		case R.id.button_del:
			performKeyDown(KeyEvent.KEYCODE_DEL);
			break;
		default:
			break;
		}
		
	}
	//模拟键盘输入
	public void performKeyDown(final int keyCode) {
		new Thread() {
			public void run() {
				try {
					Instrumentation inst = new Instrumentation();
					inst.sendKeyDownUpSync(keyCode);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
2:布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F0F1EE"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/input_buttons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button_1"
                style="@style/input_button_style"
                android:text="1" />

            <Button
                android:id="@+id/button_2"
                style="@style/input_button_style"
                android:text="2" />

            <Button
                android:id="@+id/button_3"
                style="@style/input_button_style"
                android:text="3" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button_4"
                style="@style/input_button_style"
                android:text="4" />

            <Button
                android:id="@+id/button_5"
                style="@style/input_button_style"
                android:text="5" />

            <Button
                android:id="@+id/button_6"
                style="@style/input_button_style"
                android:text="6" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button_7"
                style="@style/input_button_style"
                android:text="7" />

            <Button
                android:id="@+id/button_8"
                style="@style/input_button_style"
                android:text="8" />

            <Button
                android:id="@+id/button_9"
                style="@style/input_button_style"
                android:text="9" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button_point"
                style="@style/input_button_style"
                android:text="." />

            <Button
                android:id="@+id/button_0"
                style="@style/input_button_style"
                android:text="0" />

            <Button
                android:id="@+id/button_del"
                style="@style/input_button_style"
                android:text="删除" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
3:使用方法:在你的 FragmentActivity的布局中加入

<fragment
	            android:id="@+id/keyboard_fragment"
	            android:layout_width="fill_parent"
	            android:layout_height="fill_parent"
	            class="com.ui.more.MyKeyBoardFragment" />//注意这里的class要根据自己的包名改一下
4:可能会遇到的问题和解决方案

error inflating class fragment--

Fragment引用的包是:
import android.support.v4.app.Fragment;
而不是:
import android.app.Fragment;
然后Activity必须是继承FragmentActivity
import android.support.v4.app.FragmentActivity;
注意:如果在同一个FragmentActivity中多次使用此键盘,记住要设置成不同id,否则系统会只显示其中一个。























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值