展示:
点击Editext不会弹出键盘,点击ADD_1光标在的Editext 填充1。点击Back则撤回一个单位
自定义View代码:
public class InputTest extends LinearLayout {
private static final String TAG = "InputTest:";
private EditText edt_one,edt_two;
private Button btn_one,btn_two;
public InputTest(Context context) {
this(context,null);
}
public InputTest(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public InputTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View inflate = LayoutInflater.from(context).inflate(R.layout.input_test, this, true);
init();
setDisKeyborad();
setOnclick();
}
private void init() {
edt_one=findViewById(R.id.edt_one);
edt_two=findViewById(R.id.edt_two);
btn_one=findViewById(R.id.btn_one);
btn_two=findViewById(R.id.btn_two);
}
private void setDisKeyborad() {
edt_one.setShowSoftInputOnFocus(false);
edt_two.setShowSoftInputOnFocus(false);
}
private void setOnclick() {
//点击增加
btn_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText focusEdt=getFocusEdit();
if(focusEdt!=null){
//获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象
Editable text = focusEdt.getText();
//获得光标后的位置
int selectionEnd = focusEdt.getSelectionEnd();
Log.e(TAG,""+selectionEnd);
text.insert(selectionEnd,String.valueOf(1));
}
}
});
//点击删除
btn_two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText focusEdt=getFocusEdit();
if(focusEdt!=null){
int selectionEnd = focusEdt.getSelectionEnd();
Editable text = focusEdt.getText();
if(selectionEnd>0){
text.delete(selectionEnd-1,selectionEnd);
}
}
}
});
}
//获取当前有焦点的输入框(使用要注意判空)
private EditText getFocusEdit(){
View focus = findFocus();
if(focus instanceof EditText){
return (EditText)focus;
}
return null;
}
}
重点代码:
获取当前有焦点的输入框(使用要注意判空)。findFocus()这个如果没有弹出来要注意当前类是不是在View类里面。
//获取当前有焦点的输入框(使用要注意判空)
private EditText getFocusEdit(){
View focus = findFocus();
if(focus instanceof EditText){
return (EditText)focus;
}
return null;
}
获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象。
//获得当前Edittext,这里没有toString(),因为toString后不是同一个对象。Editable操作的是同一个对象
Editable text = focusEdt.getText();
获得光标后的位置(改为Start为获得光标前的位置)
//获得光标后的位置
int selectionEnd = focusEdt.getSelectionEnd();
自定义View布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt_one"
android:hint="one"/>
<EditText
android:layout_width="match_parent"
android:hint="two"
android:id="@+id/edt_two"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_one"
android:text="add_1"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>