标签:
引子
// filename : custom_numeric_pad.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:weightSum="3" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/pad_1" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="1"/> <Button android:id="@+id/pad_2" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="2"/> <Button android:id="@+id/pad_3" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="3"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:weightSum="3" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/pad_4" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="4"/> <Button android:id="@+id/pad_5" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="5"/> <Button android:id="@+id/pad_6" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="6"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:weightSum="3" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/pad_7" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="7"/> <Button android:id="@+id/pad_8" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="8"/> <Button android:id="@+id/pad_9" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="9"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:weightSum="3" android:layout_width="match_parent" android:layout_height="wrap_content"> android:layout_height="wrap_content"> <Button android:id="@+id/pad_dot" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="."/> <Button android:id="@+id/pad_0" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:textSize="26sp" android:layout_margin="10dp" android:gravity="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:text="0"/> <ImageButton android:id="@+id/pad_del" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:layout_margin="10dp" android:scaleType="center" android:background="@drawable/bg_cal_btn_selector" android:textColor="@color/btn_text_color" android:src="@drawable/cal_del_selector"/> </LinearLayout> </LinearLayout>
public NumericPad(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mRoot = (LinearLayout) mInflater.inflate(R.layout.custom_numeric_pad, this); }
public void setLayout(@LayoutRes int layoutId) { if (layoutId != 0) { // use customized layout from parameter mRoot = (LinearLayout) mInflater.inflate(layoutId, this); } }
因为每个小键盘按钮都是个button,那么这里首先我们要处理的是点击事件的监听,这里我们不需要创建出来button,只需要依次迭代每个ViewID对他findviewbyid出来,同时设置setonclicklistener或者setonlongclicklistener即可:
public class NumericPad extends LinearLayout implements View.OnClickListener, View.OnLongClickListener{ ... }
//initView方法需要在NumericPad的构造函数和setLayout方法中调用 private void initView() { mViewsId = new ArrayList<>(); mViewsId.add(R.id.pad_0); mViewsId.add(R.id.pad_1); mViewsId.add(R.id.pad_2); mViewsId.add(R.id.pad_3); mViewsId.add(R.id.pad_4); mViewsId.add(R.id.pad_5); mViewsId.add(R.id.pad_6); mViewsId.add(R.id.pad_7); mViewsId.add(R.id.pad_8); mViewsId.add(R.id.pad_9); mViewsId.add(R.id.pad_dot); mViewsId.add(R.id.pad_del); for (int viewId : mViewsId) { findViewById(viewId).setOnClickListener(this); findViewById(viewId).setOnLongClickListener(this); } }
@Override public boolean onLongClick(View v) { switch (v.getId()) { case R.id.pad_del: if (mInputView != null) { mInputView.setText(""); } break; } return true; } @Override public void onClick(View v) { int result_param = PAD_ERR; switch (v.getId()) { case R.id.pad_0: result_param = PAD_ZERO; break; case R.id.pad_1: result_param = PAD_ONE; break; case R.id.pad_2: result_param = PAD_TWO; break; case R.id.pad_3: result_param = PAD_THREE; break; case R.id.pad_4: result_param = PAD_FOUR; break; case R.id.pad_5: result_param = PAD_FIVE; break; case R.id.pad_6: result_param = PAD_SIX; break; case R.id.pad_7: result_param = PAD_SEVEN; break; case R.id.pad_8: result_param = PAD_EIGHT; break; case R.id.pad_9: result_param = PAD_NINE; break; case R.id.pad_dot: result_param = PAD_DOT; break; case R.id.pad_del: result_param = PAD_DEL; break; } L.d("click button type : " + result_param); // do the default action doDefaultNumericAction(result_param); }
public interface NumericPadButtonClickListener { /** * 当按下数字键盘某个按钮后的回调 * @param type 检查返回值是否为PAD_ERR(错误),否者就是正常按下了某个值,根据值来判断做对应的界面响应 */ void onNumericPadButtonClicked(int type); } /** * 设置键盘按钮响应回调方法 * @param mListener */ public void setNumericPadButtonClickListener(NumericPadButtonClickListener mListener) { this.mListener = mListener; }
@Override public void onNumericPadButtonClicked(int type) { }
@Override public void onClick(View v) { int result_param = PAD_ERR; switch (v.getId()) { case R.id.pad_0: result_param = PAD_ZERO; break; case R.id.pad_1: result_param = PAD_ONE; break; case R.id.pad_2: result_param = PAD_TWO; break; case R.id.pad_3: result_param = PAD_THREE; break; case R.id.pad_4: result_param = PAD_FOUR; break; case R.id.pad_5: result_param = PAD_FIVE; break; case R.id.pad_6: result_param = PAD_SIX; break; case R.id.pad_7: result_param = PAD_SEVEN; break; case R.id.pad_8: result_param = PAD_EIGHT; break; case R.id.pad_9: result_param = PAD_NINE; break; case R.id.pad_dot: result_param = PAD_DOT; break; case R.id.pad_del: result_param = PAD_DEL; break; } L.d("click button type : " + result_param); if (mListener != null) { // 新添加的代码 // do the custom action mListener.onNumericPadButtonClicked(result_param); } else { L.d("button click listener is null, will call the default action"); // do the default action doDefaultNumericAction(result_param); } }
设置正在操作的输入框
这里很简单,就是一个设置的接口:
public void setInputView(@NonNull TextView view) { this.mInputView = view; }
至此,一个完整的可以快速复用定制的小键盘输入组件就搞定了
标签:
原文地址:http://www.cnblogs.com/soaringEveryday/p/4739925.html