Java 如何创建可单击的编辑文本?
我知道有很多类似的问题,但没有一个适合我。我想制作一个类似于Java 如何创建可单击的编辑文本?,java,android,android-edittext,onclicklistener,Java,Android,Android Edittext,Onclicklistener,我知道有很多类似的问题,但没有一个适合我。我想制作一个类似于按钮的编辑文本。我希望它是不可编辑的(没有键盘应该弹出)。但是,当我设置onClickListener()时,EditText不会对我的onClick()事件做出反应。我应该如何正确地执行此操作 在我的xml中,我禁用了focusableInTouchMode,并将clickable设置为true。生成的EditText不可编辑,但它不会响应我的onClickListener() 编辑文本的我的xml: <EditText
按钮的编辑文本
。我希望它是不可编辑的(没有键盘应该弹出)。但是,当我设置onClickListener()
时,EditText
不会对我的onClick()
事件做出反应。我应该如何正确地执行此操作
在我的xml中,我禁用了focusableInTouchMode
,并将clickable
设置为true。生成的EditText
不可编辑,但它不会响应我的onClickListener()
编辑文本的我的xml
:
<EditText
android:id="@+id/taskviewer_date"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textColorHint="@color/colorShadow"
android:inputType="none"
android:hint="No Due Date"/>
使用onTouch事件或在EditText上放置可单击的框架布局以捕获单击事件。当您希望edittext可编辑时,使FrameLayout的可见性消失使用onTouch事件或在edittext上放置可单击的FrameLayout以捕获单击事件。如果希望edittext可编辑,请使FrameLayout的可见性消失如果将setTextIsSelectable
设置为true,则使用edittext的setOnClickListener
将起作用
在XML中:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
如果将setTextIsSelectable
设置为true,则可以使用EditText的setOnClickListener
在XML中:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
如果不希望用户编辑文本,为什么要使用EditText。改用TextView?改用OnTouchListener,看看是否有效为了简单起见,我想要EditText的外观,我希望用户以后能够编辑它。如果您确实需要使用Edittext,您可以尝试触摸事件,或者您可以在Edittext上放置一个大小相同的可单击框架布局,然后从中获取单击事件,这样框架布局就可以完成这项工作。当您希望用户能够编辑文本时,只需使其可见性消失。如果您不希望用户编辑文本,为什么要使用EditText。改用TextView?改用OnTouchListener,看看是否有效为了简单起见,我想要EditText的外观,我希望用户以后能够编辑它。如果您确实需要使用Edittext,您可以尝试触摸事件,或者您可以在Edittext上放置一个大小相同的可单击框架布局,然后从中获取单击事件,这样框架布局就可以完成这项工作。当您希望用户能够编辑时,只需使其可见性消失即可
mTextInpuEditText.setEnabled(true);
mTextInpuEditText.setTextIsSelectable(true);
mTextInpuEditText.setFocusable(false);
mTextInpuEditText.setFocusableInTouchMode(false);
mTextInpuEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Edit Text is clicked");
}
});