編輯:關於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); } }
今天因為工作需要,把以前編寫的一個GPS測試程序拿出來重新修改了一下。這個程序說起來有些歷史了,是我11年編寫的,那時候學了Android開發沒多久,算是一個實驗性的作品
適當的添加一些動畫效果,能夠獲得更好的用戶體驗,這次講講一些常見的動畫~如:透明動畫,漸變動畫等等。先看一下運行截圖:package com.vrinux.animoti
通過本文帶大家一起看過UIL這個國內外大牛都追捧的圖片緩存類庫的緩存處理機制。看了UIL中的緩存實現,才發現其實這個東西不難,沒有太多的進程調度,沒有各種內存讀取控制機制
概述:很早之前就想研究一下Android中的塗鴉,其實也說不上是研究了,畢竟都是一些相對比較簡單的知識點。下面就對基於畫布(Canvas)和觸摸事件(onTouchEve