Android custom keyboard
Customize the digital keyboard for business requirements and write a demo
Main Code:
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 stubthis.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;}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubint 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 stubsuper. 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 StartPopu PWindow (View view) {if (mPopupWindow! = Null &&! MPopupWindow. isShowing () {mPopupWindow. showAtLocation (view, Gravity. BOTTOM, 0, 0) ;}} public void StopPopupWindow () {if (mPopupWindow! = Null & mPopupWindow. isShowing () {mPopupWindow. dismiss () ;}/// add public void addString (EditText editText, String sequence) {int index = getEditSelection (editText) to the text box ); // cursor position if (index <0 | index> = getEditTextViewString (editText ). length () {editText. append (sequence);} else {editText. getEditableText (). insert (index, sequence); // insert text at the cursor position} // obtain the cursor's current position public int getEditSelection (EditText editTex T) {return editText. getSelectionStart ();} // get the public String getEditTextViewString (EditText editText) {return editText. getText (). toString ();} // clear the public void clearText (EditText editText) {editText. getText (). clear () ;}// Delete the public void deleteEditValue (EditText editText, int index) {editText. getText (). delete (index-1, index);} // set the cursor position public void setEditSelectionLoc (EditText editTex T, int index) {editText. setSelection (index);} // determines whether the value is 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;@Overridepublic int getCurrentView() {// TODO Auto-generated method stubreturn R.layout.activity_main;}@Overridepublic void initView() {// TODO Auto-generated method stubinitPopupWindow();PopupWindowUtil.getInstance().setOnTouchListener(editText, mPopupWindow, this);}@Overridepublic void showPopuWindow() {// TODO Auto-generated method stubsuper.showPopuWindow();StartPopupWindow(parent);}@Overridepublic void dismiss() {// TODO Auto-generated method stubsuper.dismiss();StopPopupWindow();}@Overridepublic void delete() {// TODO Auto-generated method stubsuper.delete();if(editText.getText().toString().trim().length()>0){deleteEditValue(editText, getEditSelection(editText));}}@Overridepublic void addString(String str) {// TODO Auto-generated method stubsuper.addString(str);addString(editText, str);}}
This is the basic code. If you need a password keyboard (only numbers), add a callback for the addString parameter. Hiding the system keyboard cannot be hidden in the later version. In the end, I used another idea to directly disable the InputType pop-up of the system input method. TYPE_NULL, legacy problem: when the cursor locates the OnTouchListener, the cursor is positioned everywhere to be resolved.