Android 自定义数字键盘

业务需求自定义数字键盘,写了一个demo

主要代码:

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.caculater.PopupWindowUtil.OnPopuWindowListener;
import com.example.caculaternumber.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class CaculaterPopuWindow{

	private static Activity mActivity;
	private static CaculaterPopuWindow mPopupWindow;

	private String phone="";

	@ViewInject(R.id.caculater_del)
	private ImageView caculaterdel;

	@ViewInject(R.id.caculater_p)
	private ImageView caculaterp;

	@ViewInject(R.id.caculater_0)
	private ImageView caculater0;

	@ViewInject(R.id.caculater_1)
	private ImageView caculater1;

	@ViewInject(R.id.caculater_2)
	private ImageView caculater2;

	@ViewInject(R.id.caculater_3)
	private ImageView caculater3;

	@ViewInject(R.id.caculater_4)
	private ImageView caculater4;

	@ViewInject(R.id.caculater_5)
	private ImageView caculater5;

	@ViewInject(R.id.caculater_6)
	private ImageView caculater6;

	@ViewInject(R.id.caculater_7)
	private ImageView caculater7;

	@ViewInject(R.id.caculater_8)
	private ImageView caculater8;

	@ViewInject(R.id.caculater_9)
	private ImageView caculater9;

	@ViewInject(R.id.editText)
	private EditText editText;
	private OnPopuWindowListener listener;
	private int mScreenWidth;
	private int mScreenHeight;

	public CaculaterPopuWindow(){
	}

	public static CaculaterPopuWindow getInstance(Activity activity){
		mActivity=activity;

		if(mPopupWindow==null){
			mPopupWindow=new CaculaterPopuWindow();
		}
		return mPopupWindow;
	}

	public PopupWindow onCreate(OnPopuWindowListener listener) {
		// TODO Auto-generated method stub
		this.listener=listener;
		View view=mActivity.getLayoutInflater().inflate(R.layout.include_caculater, null);
		ViewUtils.inject(this, view);
		initScreen();
		int height=mScreenHeight/3;
		PopupWindow pw=new PopupWindow(view,LayoutParams.MATCH_PARENT,height);
		pw.setBackgroundDrawable(new ColorDrawable(0));
		pw.setOutsideTouchable(true);
		return pw;
	}

	@OnClick({R.id.caculater_0,R.id.caculater_1,R.id.caculater_2,R.id.caculater_3,R.id.caculater_4,R.id.caculater_5,R.id.caculater_6,R.id.caculater_7,R.id.caculater_8,R.id.caculater_9,R.id.caculater_p,R.id.caculater_del})
	private void clickMethod(View v){

		if(v==caculaterdel){
			listener.delete();
		}else{
			if(v==caculater0){
				phone="0";
			}else if(v==caculater1){
				phone="1";
			}else if(v==caculater2){
				phone="2";
			}else if(v==caculater3){
				phone="3";
			}else if(v==caculater4){
				phone="4";
			}else if(v==caculater5){
				phone="5";
			}else if(v==caculater6){
				phone="6";
			}else if(v==caculater7){
				phone="7";
			}else if(v==caculater8){
				phone="8";
			}else if(v==caculater9){
				phone="9";
			}else if(v==caculaterp){
				phone=".";
			}

			listener.addString(phone);
		}
	}

	public void initScreen(){

		WindowManager manager=(WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics=new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);

		mScreenWidth=outMetrics.widthPixels;
		mScreenHeight=outMetrics.heightPixels;
	}
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;

public class PopupWindowUtil implements android.view.View.OnTouchListener{

	private static PopupWindowUtil util;
	private EditText mEditText;
	private PopupWindow mPopuWindow;
	private OnPopuWindowListener listener;
	public PopupWindowUtil(){

	}
	public static PopupWindowUtil getInstance(){
		if(util==null){
			util=new PopupWindowUtil();
		}
		return util;
	}

	public void setOnTouchListener(EditText mEditText,PopupWindow mPopuWindow,OnPopuWindowListener listener){
		mEditText.setOnTouchListener(this);
		this.mEditText=mEditText;
		this.mPopuWindow=mPopuWindow;
		this.listener=listener;
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int inputType=mEditText.getInputType();
		mEditText.setInputType(InputType.TYPE_NULL);
		mEditText.setInputType(inputType);
		mEditText.setSelection(mEditText.getText().toString().trim().length());

		if(mPopuWindow!=null&&!mPopuWindow.isShowing()){
			listener.showPopuWindow();
		}

		return false;
	}

	public interface OnPopuWindowListener{
		void showPopuWindow();
		void dismiss();
		void addString(String str);
		void delete();
	}
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.example.caculater.PopupWindowUtil.OnPopuWindowListener;
import com.lidroid.xutils.ViewUtils;

public abstract class BaseActivity extends Activity implements OnPopuWindowListener{
	protected PopupWindow mPopupWindow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(getCurrentView());
		ViewUtils.inject(this);
		initView();
	}

	public abstract int getCurrentView();
	public abstract void initView();

	public void initPopupWindow(){
		if(mPopupWindow==null){
			mPopupWindow=CaculaterPopuWindow.getInstance(this).onCreate(this);
		}
	}
	public void StartPopupWindow(View view){
            if(mPopupWindow!=null&&!mPopupWindow.isShowing()){
            	mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
            }

	}

	public void StopPopupWindow(){
		 if(mPopupWindow!=null&&mPopupWindow.isShowing()){
         	mPopupWindow.dismiss();
         }
	}

	// 往文本框中添加内容
	public void addString(EditText editText,String sequence) {
		int index = getEditSelection(editText);// 光标的位置
		if (index < 0 || index >= getEditTextViewString(editText).length()) {
			editText.append(sequence);
		} else {
			editText.getEditableText().insert(index, sequence);// 光标所在位置插入文字
		}
	}

	// 获取光标当前位置
	public int getEditSelection(EditText editText) {
		return editText.getSelectionStart();
	}

	// 获取文本框的内容
	public String getEditTextViewString(EditText editText) {
		return editText.getText().toString();
	}

	// 清除文本框中的内容
	public void clearText(EditText editText) {
		editText.getText().clear();
	}

	// 删除指定位置的字符
	public void deleteEditValue(EditText editText,int index) {
		editText.getText().delete(index - 1, index);
	}

	// 设置光标位置
	public void setEditSelectionLoc(EditText editText,int index) {
		editText.setSelection(index);
	}

	// 判断是否是数字
	public static boolean isNum(String str) {
		return str.matches("([0-9]+)?)$");
	}

	public void showPopuWindow(){

	}
	public void dismiss(){

	}
	public void addString(String str){

	}
	public void delete(){

	}
}
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.caculater.BaseActivity;
import com.example.caculater.PopupWindowUtil;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MainActivity extends BaseActivity {

	@ViewInject(R.id.editText)
	private EditText editText;

	@ViewInject(R.id.parent)
	private LinearLayout parent;

	@Override
	public int getCurrentView() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		initPopupWindow();
		PopupWindowUtil.getInstance().setOnTouchListener(editText, mPopupWindow, this);
	}

	@Override
	public void showPopuWindow() {
		// TODO Auto-generated method stub
		super.showPopuWindow();
		StartPopupWindow(parent);
	}
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
		StopPopupWindow();
	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub
		super.delete();
		if(editText.getText().toString().trim().length()>0){
			deleteEditValue(editText, getEditSelection(editText));
		}
	}
	@Override
	public void addString(String str) {
		// TODO Auto-generated method stub
		super.addString(str);
		addString(editText, str);
	}

}

基本代码就这些,如果需要密码键盘(纯数字),添加一个addString 参数的回调即可。隐藏系统键盘在高版本无法隐藏,最后换了一种思路,直接禁止了系统输入法的弹出  InputType.TYPE_NULL  ,遗留问题:光标定位  OnTouchListener时将光标定位到处摸位置待解决

时间: 2024-11-06 03:53:27

Android 自定义数字键盘的相关文章

一个高仿闲鱼键自定义数字键盘特效

博客地址: http://www.jianshu.com/p/be68facfc4b8 仿造android端闲鱼发布选择价格数字键盘,自定义数字键盘 public class MyKeyBoardView extends KeyboardView { private Context mContext; private Keyboard mKeyBoard; public MyKeyBoardView(Context context, AttributeSet attrs) { super(con

Android自定义软键盘

前不久由于项目的需要,要做一个自定义的软键盘,我也上网看了很多,都觉得很繁琐,所以想自己动手实现个.以备不时之需把.我选择了参考百度钱包的软键盘,看起来还不错: 下面一起来实现它: 1.写一个键盘控件,这个实现起来比较简单,就不多说了 public class SoftInputBoard extends RelativeLayout implements View.OnClickListener{ private Scroller mScroller; private int mScreenH

自定义数字键盘组件化方案

实现场景:点击输入框,弹出自定义数字键盘,输入数字后点击确定,输入框中显示对应数字.如果输入框中有值,则默认带到小键盘中. 其中:数字键盘为子组件.输入框公用该子组件. 效果图如下: 难点: 1.props传入数字键盘组件中的值,无法直接修改.需要通过计算属性进行修改. 2.公用键盘时,保证符合预期,数字不会错乱.使用v-for,巧用name属性,通过点击时更改传入的参数,达到目的. 下面时完整代码: index.vue <template> <view class="cont

iOS自定义数字键盘

自定义键盘实际就是设置UITextField的inputView属性,首先我们要做的是自定义一个UIView,实现键盘的样式. 自定义View代码如下: 1 #import <UIKit/UIKit.h> 2 //创建自定义键盘协议 3 @protocol My_KeyBoardDelegate <NSObject> 4 //创建协议方法 5 @required//必须执行的方法 6 - (void)numberKeyBoard:(NSInteger) number; 7 - (v

GridLayout自定义数字键盘(两个EditText)

功能说明: 适用于两个EditText输入框,并控制输入字符的长度,点击键盘中的"确定"按钮完成输入,点击"前一项"光标跳到前一个EditText 运行效果图如下: 布局文件 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi

html5自定义数字键盘

原理:使用div模拟输入框,避免手机原生键盘弹出,键盘使用div模拟,点击事件使用js控制,光标闪烁使用css模拟,具体代码如下: 1 <!doctype html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,ch

GridView+PopupWindow自定义数字键盘

public class SimpleGridViewTest extends Activity {EditText editText;private PopupWindow popupWindow;private GridView mGridView;private View contentview;private List<String> datas = new ArrayList<String>();GridViewAdapter adapter;private static

Android 自定义的数字键盘 支持随意拖动 和稳定的字符输入的控件

经过 研究 实现了自定义 键盘 ,支持随意拖动 和数字及其他字符输入 下面是主要的代码 和使用方法 import android.content.Context; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.Gravity; import android.view.Mo

Android自定义键盘之汉字键盘

实现软键盘主要用到了系统的两个类:Keyboard和KeyboardView. Keyboard类源码的介绍是: Listener for virtual keyboard events.即用于监听虚拟键盘. KeyboardView类源码的介绍是: A view that renders a virtual {@link Keyboard}. It handles rendering of keys and detecting key presses and touch movements.即