Java Android:CustomView中的EditText在触摸时表现不符合预期
我有一个CustomView,它包含一个LinearLayout,其中包含一个EditText&另一个自定义视图元素。但是,当我运行我的应用程序并触摸编辑文本时,它的行为与预期不符(似乎没有接收到焦点&软键盘没有打开)。我已尝试在EditText和LinearLayout上设置duplicateParentState=“true”。我还在EditText中附加了一个onTouchEventListener,它调用了一个简单的toast&toast确实出现了 这是我的CustomView的代码 form_field.xmlJava Android:CustomView中的EditText在触摸时表现不符合预期,java,android,android-edittext,android-custom-view,Java,Android,Android Edittext,Android Custom View,我有一个CustomView,它包含一个LinearLayout,其中包含一个EditText&另一个自定义视图元素。但是,当我运行我的应用程序并触摸编辑文本时,它的行为与预期不符(似乎没有接收到焦点&软键盘没有打开)。我已尝试在EditText和LinearLayout上设置duplicateParentState=“true”。我还在EditText中附加了一个onTouchEventListener,它调用了一个简单的toast&toast确实出现了 这是我的CustomView的代码 f
FormField.java
package com.example.app;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.text.InputType;
导入android.util.AttributeSet;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入java.util.ArrayList;
公共类FormField扩展了LinearLayout{
私人编辑文本;
私有验证图标验证图标;
私有整数字段输入类型;
私有字符串FieldInput;
公共表单字段(上下文)
{
超级(上下文);
}
公共表单字段(上下文上下文,属性集属性集)
{
super(上下文、属性集);
TypedArray attrs=context.actainStyleDatAttributes(attributeSet,R.styleable.FormField,0,0);
fieldInputType=attrs.getInt(
R.styleable.FormField\u fieldInputType,
InputType.TYPE\u文本\u变化\u正常
);
fieldInputHint=attrs.getString(
R.styleable.FormField\u fieldInputHint
);
attrs.recycle();
充气(getContext(),R.layout.form_字段,this);
此参数为.setFocusable(true);
此.setFocusableInTouchMode(true);
editText=(editText)findViewById(R.id.edit_text);
editText.setInputType(fieldInputType);
editText.setHint(fieldInputHint);
editText.setFocusableInTouchMode(true);
validationIcon=(validationIcon)findViewById(R.id.validation\u图标);
validationIcon.setValid(true);
ArrayList touchables=新的ArrayList();
可触摸。添加(此);
添加(编辑文本);
可添加可触摸(可触摸);
}
公共表单字段(上下文上下文、属性集属性、int-defStyle)
{
超级(上下文、属性、定义样式);
}
}
registration\u layout.xml
最小SDK版本设置为14,目标设置为20。任何帮助都将不胜感激
编辑:
我今天在我的表单字段上测试长按时收到了这个logcat消息
W/TextView﹕ TextView does not support text selection. Action mode cancelled.
我仔细检查了我的代码,EditText元素只能转换为EditText。如果EditText被转换为TextView,可以解释我最初的问题,但现在我不清楚为什么或如何转换为TextView。看起来您正在扩展LinearLayout,但从未在布局中使用它。从您发布的内容来看,您根本没有使用FormField,我看到的唯一自定义视图是ValidationIcon视图。因此,不会将触摸事件挂接到EditText和ValidationIcon中,因为在layout.xml中没有使用FormField 与其扩展LinearLayout,不如只使用预定义的方法和属性来处理您试图实现的行为。例如,显示键盘、请求焦点和显示提示:
<EditText
android:id="@+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
**android:inputType="text"**
**android:hint="@styleable/FormField_fieldInputHint"**
android:padding="10dp"
android:textColor="#2d6169"
android:textSize="18sp"
android:background="@color/transparent" />
确保未设置
输入类型:'none'
或'0X000000'自定义_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:foregroundGravity="right"
app:layout_anchorGravity="right">
<LinearLayout
android:orientation="vertical"
android:id="@+id/search_closed_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:background="@color/white"
android:foregroundGravity="center"
android:gravity="right">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_marginStart="16dp"
android:layout_marginTop="14dp"
android:fontFamily="@font/intel"
android:foregroundGravity="center"
android:text="textview"
android:textColor="@color/title_color"
android:textSize="14dp"
android:textStyle="bold" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:clickable="true"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edit_txtbg"
android:fontFamily="@font/intel"
android:gravity="center|start"
android:paddingStart="24dp"
android:textColor="@color/text_color"
android:textColorHint="@color/txt_hint"
android:textSize="@dimen/text_nev"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/img_add_patient"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</FrameLayout>
main/./attr.xml
<declare-styleable name="CustomEditText">
<attr name="title" format="string"/>
<attr name="onCenter" format="boolean"/>
<attr name="hint" format="string"/>
<attr name="maxLength" format="integer"/>
<attr name="inputType" format="string"/>
</declare-styleable>
@可绘制/编辑\u txtbg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="1dp" android:color="@color/button_bg_border" />
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="6dp"/>
<solid android:color="@color/button_bg" />
</shape>
-
在布局中使用
谢谢你我已经用一些新信息更新了我的原始问题&my registration\u layout.xml,这样你就可以看到我是如何使用FormField元素的。是的,这现在更有意义了。您确定它正在调用第二个构造函数吗?也许可以通过将自定义代码移动到一个单独的方法并在每个构造函数中调用它来进行测试?我让它工作了!正如您所说,这是对所有三个构造函数使用init方法的组合,还将我的fieldInputType默认值从
InputType.TYPE\u VARIATION\u NORMAL
更改为InputType.TYPE\u CLASS\u TEXT
。非常感谢。
<declare-styleable name="CustomEditText">
<attr name="title" format="string"/>
<attr name="onCenter" format="boolean"/>
<attr name="hint" format="string"/>
<attr name="maxLength" format="integer"/>
<attr name="inputType" format="string"/>
</declare-styleable>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="1dp" android:color="@color/button_bg_border" />
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="6dp"/>
<solid android:color="@color/button_bg" />
</shape>
<com.mdppractice.custom.CustomEditText
android:id="@+id/edt_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Patient ID/Phone"
android:autofillHints="@string/verified"
android:foregroundGravity="center"
android:hint="Please enter name"
app:inputType="textCapSentences"
android:textColor="@color/title_color"
android:textSize="14dp"
android:textStyle="bold" />