码迷,mamicode.com
首页 > 移动开发 > 详细

Android 自定义数字键盘

时间:2015-05-06 15:15:38      阅读:227      评论:0      收藏:0      [点我收藏+]

标签: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时将光标定位到处摸位置待解决

   

Android 自定义数字键盘

标签:android   自定义数字键盘   

原文地址:http://blog.csdn.net/anddroid_lanyan/article/details/45534579

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!