//布局相关 //.....自己看
editText = (EditText) mView.findViewById(R.id.qc_liangpin); text = (EditText) mView.findViewById(R.id.qc_buliangping); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Edit_Tyep = 1;//多个输入框时做的焦点判断 } }); text.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Edit_Tyep = 2; } });
Button btn0 = (Button) mView.findViewById(R.id.btn_zero); btn0.setOnClickListener(listener); Button btn1 = (Button) mView.findViewById(R.id.btn_one); btn1.setOnClickListener(listener); Button btn2 = (Button) mView.findViewById(R.id.btn_two); btn2.setOnClickListener(listener); Button btn3 = (Button) mView.findViewById(R.id.btn_three); btn3.setOnClickListener(listener); Button btn4 = (Button) mView.findViewById(R.id.btn_four); btn4.setOnClickListener(listener); Button btn5 = (Button) mView.findViewById(R.id.btn_five); btn5.setOnClickListener(listener); Button btn6 = (Button) mView.findViewById(R.id.btn_six); btn6.setOnClickListener(listener); Button btn7 = (Button) mView.findViewById(R.id.btn_seven); btn7.setOnClickListener(listener); Button btn8 = (Button) mView.findViewById(R.id.btn_eight); btn8.setOnClickListener(listener); Button btn9 = (Button) mView.findViewById(R.id.btn_nine); btn9.setOnClickListener(listener); Button btndel = (Button) mView.findViewById(R.id.btn_del); btndel.setOnClickListener(listener); Button btnclear = (Button) mView.findViewById(R.id.btn_clear); btnclear.setOnClickListener(listener);
Button.OnClickListener listener = new Button.OnClickListener() { public void onClick(View v) { Editable editable = null; int start = 0; if (Edit_Tyep == 1) { editable = editText.getText(); start = editText.getSelectionStart(); } else if (Edit_Tyep == 2) { editable = text.getText(); start = text.getSelectionStart(); } else { editable = editText.getText(); start = editText.getSelectionStart(); } switch (v.getId()) { case R.id.btn_zero: editable.insert(start, "0"); break; case R.id.btn_one: editable.insert(start, "1"); break; case R.id.btn_two: editable.insert(start, "2"); break; case R.id.btn_three: editable.insert(start, "3"); break; case R.id.btn_four: editable.insert(start, "4"); break; case R.id.btn_five: editable.insert(start, "5"); break; case R.id.btn_six: editable.insert(start, "6"); break; case R.id.btn_seven: editable.insert(start, "7"); break; case R.id.btn_eight: editable.insert(start, "8"); break; case R.id.btn_nine: editable.insert(start, "9"); break; case R.id.btn_clear: if (editable != null) editable.delete(0, editable.length()); break; case R.id.btn_del: if (start > 0) editable.delete(start - 1, start); break; } } };
由于时间问题没有 给全代码 关键地方用颜色标出 其实很简单 相信应该能够看懂!