Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自定義數字鍵盤

Android 自定義數字鍵盤

編輯:關於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時將光標定位到處摸位置待解決

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved