自定义悬浮在界面上的数字键盘

近期由于项目中很多EditText控件都是输入纯数字,不停的弹出和关闭系统自带的键盘体验很差,所以决定做一款悬浮在EditText控件旁边的数字小键盘。
当用户点击EditText控件的时候会弹出数字键盘,用户输入完成之后点击确定会把输入的内容显示在EditText中,这样就避免了弹出系统键盘这种不友好的体验

先上效果图




这个demo是利用WindowManager实现的,本来想用Popupwindow的,发现限制太大,决定用WindowManager实现,但是也有个缺陷,不好控制键盘的位置,可能是我现学现用,理解的不是很全面,知道更好方法的朋友可以交流

上代码
先创建一个自定义键盘控件

import android.content.Context;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class DigitPasswordKeyPad extends View {
    private Context ctx = null;
    private View v;
    private String digitnum = "";
    private int length = 20;
 
    private Button digitkeypad_1;
    private Button digitkeypad_2;
    private Button digitkeypad_3;
    private Button digitkeypad_4;
    private Button digitkeypad_5;
    private Button digitkeypad_6;
    private Button digitkeypad_7;
    private Button digitkeypad_8;
    private Button digitkeypad_9;
    private Button digitkeypad_0;
    private Button digitkeypad_c;
    private Button digitkeypad_ok;
    private Button digitkeypad_point;
    private EditText digitkeypad_edittext;
 
    private boolean isPwd;
 
    public DigitPasswordKeyPad(Context ctx) {
        super(ctx);
        this.ctx = ctx;
    }
 
    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }
 
    public void setEditTextIsPwd(boolean ispwd) {
        if (ispwd) {
            digitkeypad_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            digitkeypad_edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        this.isPwd = ispwd;
    }
 
    public View setup() {
        LayoutInflater lif = LayoutInflater.from(ctx);
        v = lif.inflate(R.layout.activity_digit_input_number, null);
 
        // 初始化 对象
        digitkeypad_1 = (Button) v.findViewById(R.id.digitkeypad_1);
        digitkeypad_2 = (Button) v.findViewById(R.id.digitkeypad_2);
        digitkeypad_3 = (Button) v.findViewById(R.id.digitkeypad_3);
        digitkeypad_4 = (Button) v.findViewById(R.id.digitkeypad_4);
        digitkeypad_5 = (Button) v.findViewById(R.id.digitkeypad_5);
        digitkeypad_6 = (Button) v.findViewById(R.id.digitkeypad_6);
        digitkeypad_7 = (Button) v.findViewById(R.id.digitkeypad_7);
        digitkeypad_8 = (Button) v.findViewById(R.id.digitkeypad_8);
        digitkeypad_9 = (Button) v.findViewById(R.id.digitkeypad_9);
        digitkeypad_0 = (Button) v.findViewById(R.id.digitkeypad_0);
        digitkeypad_point = (Button) v.findViewById(R.id.digitkeypad_point);
        
        digitkeypad_c = (Button) v.findViewById(R.id.digitkeypad_c);
        digitkeypad_ok = (Button) v.findViewById(R.id.digitkeypad_ok);
        digitkeypad_edittext = (EditText) v.findViewById(R.id.digitpadedittext);
 
        // 添加点击事件
        DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener();
        digitkeypad_1.setOnClickListener(dkol);
        digitkeypad_2.setOnClickListener(dkol);
        digitkeypad_3.setOnClickListener(dkol);
        digitkeypad_4.setOnClickListener(dkol);
        digitkeypad_5.setOnClickListener(dkol);
        digitkeypad_6.setOnClickListener(dkol);
        digitkeypad_7.setOnClickListener(dkol);
        digitkeypad_8.setOnClickListener(dkol);
        digitkeypad_9.setOnClickListener(dkol);
        digitkeypad_0.setOnClickListener(dkol);
        digitkeypad_point.setOnClickListener(dkol);
        digitkeypad_c.setOnClickListener(dkol);
        digitkeypad_ok.setOnClickListener(new DigitPasswordKeypadFinshOnClikcListener());
 
        return v;
    }
 
    private class DigitPasswordKeypadFinshOnClikcListener implements OnClickListener {
 
        @Override
        public void onClick(View view) {
            int viewId = view.getId();
 
            if (viewId == R.id.digitkeypad_ok) {
                // 点击完成
                // 设置值回页面
                // 隐藏自己View
//                 if (isPwd) {
//                	 jsimpl.hidePasswdPad(digitkeypad_edittext.getText().toString());
//                 } else {
//                	 jsimpl.hideCallNumPad(digitkeypad_edittext.getText().toString());
//                 }
            	Log.i("aaaaaaaaaaaaaa", "bbbbbbbbbbbb");
//                v.setVisibility(View.GONE);
            }
        }
    }
 
    public void initInputLable(String str, int length) {
        str = str.trim();
        digitnum = str;
        this.length = length;
        digitkeypad_edittext.setText(digitnum);
        digitkeypad_edittext.setSelection(digitnum.length());
    }
 
    private class DigitPasswordKeypadOnClickListener implements OnClickListener {
 
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
 
            switch (viewId) {
            case R.id.digitkeypad_1:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 1;
                }
                break;
            case R.id.digitkeypad_2:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 2;
                }
                break;
            case R.id.digitkeypad_3:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 3;
                }
                break;
            case R.id.digitkeypad_4:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 4;
                }
                break;
            case R.id.digitkeypad_5:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 5;
                }
                break;
            case R.id.digitkeypad_6:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 6;
                }
                break;
            case R.id.digitkeypad_7:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 7;
                }
                break;
            case R.id.digitkeypad_8:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 8;
                }
                break;
            case R.id.digitkeypad_9:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 9;
                }
                break;
            case R.id.digitkeypad_0:
                if (digitnum.length() == length) {
                    return;
                } else {
                    digitnum += 0;
                }
 
                break;
            case R.id.digitkeypad_point:
            	if (digitnum.length() == length) {
            		return;
            	} else {
            		digitnum += ".";
            	}
            	
            	break;
            case R.id.digitkeypad_c:// 后退
                if (digitnum.length() > 0) {
                    digitnum = digitnum.substring(0, digitnum.length() - 1);
                }
                break;
            }
            // 格式化 数据
            digitkeypad_edittext.setText(digitnum);
            digitkeypad_edittext.setSelection(null != digitnum ? digitnum.length() : 0);
        }
 
    }
}



然后用WindowManager或者PopupWindow初始化这个组件,这里两种方法都给出了

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;

public class DigitKeyPadUtil {
	public static void showPassWdPadView(final EditText tv, final Context context,
			final View digitView) {
				// 让一个视图浮动在你的应用程序之上
				final WindowManager windowmanager = (WindowManager) context
						.getSystemService(Context.WINDOW_SERVICE);
				LayoutParams layoutparams = new LayoutParams(400, 550,
						WindowManager.LayoutParams.FIRST_SUB_WINDOW,
						WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
						PixelFormat.RGBA_8888);
				layoutparams.gravity = Gravity.LEFT;
				layoutparams.x = 200;
				layoutparams.y = 500;

				Button btn = (Button) digitView
						.findViewById(R.id.digitkeypad_ok);
				final EditText digitkeypad_edittext = (EditText) digitView
						.findViewById(R.id.digitpadedittext);
				btn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						String str = digitkeypad_edittext.getText().toString();
						tv.setText(str);
						if (digitView.getParent() != null) {
							windowmanager.removeView(digitView);
						}
					}
				});

				if (digitView.getParent() == null) {
					windowmanager.addView(digitView, layoutparams);
				}
				// LayoutInflater mLayoutInflater = (LayoutInflater)
				// getSystemService(LAYOUT_INFLATER_SERVICE);
				// 自定义布局
				// final PopupWindow mPop = new PopupWindow(passwdview,
				// LayoutParams.WRAP_CONTENT,
				// LayoutParams.WRAP_CONTENT, true);
				// mPop.setContentView(passwdview);//设置包含视图
				// mPop.setWidth(300);
				// mPop.setHeight(400);//设置弹出框大小
				// mPop.setOutsideTouchable(false);
				// mPop.showAsDropDown(editText);
				//
				// Button btn = (Button)
				// passwdview.findViewById(R.id.digitkeypad_ok);
				// final EditText digitkeypad_edittext = (EditText)
				// passwdview.findViewById(R.id.digitpadedittext);
				// btn.setOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View arg0) {
				// String str = digitkeypad_edittext.getText().toString();
				// editText.setText(str);
				// mPop.dismiss();
				// }
				// });
	}

}



在activity中很简单,只需要给需要弹出数字键盘的组件设置事件就行

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
 
public class AndroidInputNumberActivity extends Activity {
    private DigitPasswordKeyPad dpk;
    private View digitView;
    private Context content ;
    EditText editText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        content = this;
        
        dpk = new DigitPasswordKeyPad(this);
        digitView = dpk.setup();
        
        editText = (EditText)findViewById(R.id.input);
        editText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	DigitKeyPadUtil.showPassWdPadView((EditText)v,content,digitView);
            }
        });
    }
    

}



activity_digit_input_number.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/digitkeypadrootlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/digitpadedittext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:editable="false"
                android:gravity="center_vertical|right"
                android:singleLine="true" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/digitkeypad_1"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/digitKeyppad_one"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_2"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/digitKeyppad_two"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_3"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/digitKeyppad_three"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_4"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_four"
                android:textSize="22sp" >
            </Button>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/digitkeypad_5"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_five"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_6"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_six"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_7"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_seven"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_8"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_eight"
                android:textSize="22sp" >
            </Button>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/digitkeypad_9"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_nine"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_0"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_zero"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_point"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_point"
                android:textSize="22sp" >
            </Button>

            <Button
                android:id="@+id/digitkeypad_c"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:text="@string/digitKeyppad_delete"
                android:textSize="22sp" >
            </Button>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal" 
            android:gravity="center"
            >

            <Button
                android:id="@+id/digitkeypad_ok"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="@string/digitKeyppad_ok"
                android:textSize="22sp" >
            </Button>
        </LinearLayout>

</LinearLayout>



最后别忘了在配置文件中加入权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />



这样就可以了,后续想维护加入拖动什么的都很方便

博客地址:http://qiaoyihang.iteye.com/

posted @ 2014-08-28 20:34  George_sz  Views(618)  Comments(0Edit  收藏  举报