简体   繁体   English

如何创建可点击的Edittext?

[英]How to create a clickable Edittext?

I know a there is a lot of similar questions out there, but none of them work for me. 我知道那里有很多类似的问题,但是没有一个对我有用。 I want to make a EditText that acts like a Button . 我想制作一个类似ButtonEditText I want it to be not editable (and no keyboard should popup). 我希望它不可编辑(并且不应该弹出键盘)。 However, when I set an onClickListener() , the EditText doesn't react to my onClick() event. 但是,当我设置onClickListener()EditText对我的onClick()事件没有反应。 How should I do this correctly? 我应该如何正确地做到这一点?

In my xml I disabled the focusableInTouchMode and I set the clickable to be true. 在我的xml中,我禁用了focusableInTouchMode ,并将clickable设置为true。 My resulting EditText is not editable, but it doesn't response to my onClickListener() . 我产生的EditText是不可编辑的,但是没有响应我的onClickListener()

My xml of the EditText : 我的EditText 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"/>

My Code: 我的代码:

EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("taskviewer", "OnClickListener called !");
        }
    }
);

Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. 使用onTouch事件或将可单击的FrameLayout放在EditText上以捕获单击事件。 Make FrameLayout's visibility Gone when you want your edittext to be editable 当您希望编辑文本可编辑时,使FrameLayout的可见性消失

Use of EditText's setOnClickListener will work if you set setTextIsSelectable to true. 如果将setTextIsSelectable设置为true,则可以使用EditText的setOnClickListener

In the XML: 在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>

In the appropriate fragment or the activity : 在适当的片段或活动中

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");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM