如何使编辑文本在JAVA中可点击而不是可编辑

时间:2022-09-11 17:01:01

Is it possible to make EditText clickable but not editable.

是否可以让EditText可点击而不是可编辑。

I don't want it to be editable (Neither should the keyboard come up nor should we be able to change the hint)

我不希望它是可编辑的(键盘也不应该出现我们也不应该改变提示)

Actually I want to use Edit text simply as an image with a hint (which cannot be changed). I know the actual way was to use an ImageView and one TextView, but I want it to try with EditText because I'll then be using only one view instead of 2. Also every thing is dynamic so no XML.

实际上,我想使用编辑文本作为一个带有提示(不能更改)的图像。我知道实际的方法是使用ImageView和一个TextView,但是我想用EditText来尝试,因为我将只使用一个视图而不是2。而且所有东西都是动态的,所以没有XML。

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java.

对于上述需求,XML中的解决方案是android:editable="false",但我想在Java中使用这个。

But,

但是,

If I use :-

如果我使用:-

et.setEnabled(false);

or

 et.setKeyListener(null);

It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

它使EditText不可编辑,但同时也使其不可单击。

8 个解决方案

#1


106  

The trick here is :-

这里的诀窍是:-

et.setFocusable(false);
et.setClickable(true);

#2


16  

you can set EditText by using java code as fallowing :-

您可以通过使用java代码来设置EditText:-。

edittext.setFocusable(false);
edittext.setClickable(true);

or by using below code in XML file for EditText.

或者在XML文件中为EditText使用下面的代码。

android:editable="false" 
android:inputType="none"  

#3


7  

You can use this..

您可以使用此. .

android:focusableInTouchMode="false"

to make edittext not editable in xml.And for clickable event you can use setOnTouchListener() event to do the same thing as like..

使edittext在xml中不可编辑。对于可单击事件,您可以使用setOnTouchListener()事件来执行相同的操作。

editText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
                        //do your stuff here..
        return false;
    }
});

#4


3  

android:editable is deprecated but you can do stg like below:

不赞成可编辑,但是你可以做如下的stg:

android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"

I used the cade above for edittexts which i use pickers to set data in. You can use input layout with this.

我使用上面的cade进行编辑,我使用拾音器来设置数据。您可以使用这个输入布局。

#5


0  

For me none of the answers resulted in a completely working solution.

对我来说,没有一个答案能产生一个完全有效的解决方案。

Showcase of my fix https://www.youtube.com/watch?v=oUA4Cuxfdqc

我的fix https://www.youtube.com/watch?v=oUA4Cuxfdqc的展示

Guide (noob-friendly), please note the few comments in the code.

指南(nobo -friendly),请注意代码中的一些注释。

Create a Java class for a custom EditText called EditTextDispatched.java

为一个名为edittextdispatch. Java的自定义编辑器创建一个Java类。

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * @author Martin Pfeffer
 * @see <a href="https://celox.io">https://celox.io</a>
 */
public class EditTextDispatched extends android.support.v7.widget.AppCompatEditText {

    private static final String TAG = "DispatchingEditText";

    private boolean editable = true;

    public EditTextDispatched(Context context) {
        super(context);
    }

    public EditTextDispatched(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextDispatched(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // get the current state
    public boolean isEditable() {
        return editable;
    }

    // set your desired behaviour
    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {

        if (editable) {
            // default behaviour of an EditText
            super.dispatchTouchEvent(motionEvent);
            return true;
        }

        // achieve the click-behaviour of a TextView (it's cuztom)
        ((RelativeLayout) getParent()).onTouchEvent(motionEvent);
        return true;
    }

}

Reference the EditTextDispatched in your xml (you will have to alter the pgk-name):

在您的xml中引用edittextsent(您必须更改pgk-name):

  <io.celox.brutus.EditTextDispatched
    android:id="@+id/row_field_value"
    style="@style/row_field_text_display"
    android:layout_alignParentBottom="true"
    android:text="@string/sample_field_value" />

To invoke:

调用:

private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) {
    value.setEditable(editable);
    value.setEnabled(editable);
}

#6


0  

edittext enable and disable for the edit like this


EditText edit = (EditText) findviewby(R.id.edittext);

through java
edit.setEnabled(false); //non editable
edit.setEnabled(true); //editable

through xml
          <EditText
                android:id="@+id/otp_mpin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:editable="false"
                android:focusable="false"/>

#7


0  

Maybe you can try

也许你可以试试

editText.setEnabled(false);

#8


-1  

you can also do android:inputMethod="@null" in your EditText and it will not show the cursor or the keyboard and then you can easily grasp the click through setting listener.

你也可以使用android:inputMethod="@null"在你的EditText中,它不会显示光标或键盘,然后你可以轻松地通过设置监听器来抓取点击。

#1


106  

The trick here is :-

这里的诀窍是:-

et.setFocusable(false);
et.setClickable(true);

#2


16  

you can set EditText by using java code as fallowing :-

您可以通过使用java代码来设置EditText:-。

edittext.setFocusable(false);
edittext.setClickable(true);

or by using below code in XML file for EditText.

或者在XML文件中为EditText使用下面的代码。

android:editable="false" 
android:inputType="none"  

#3


7  

You can use this..

您可以使用此. .

android:focusableInTouchMode="false"

to make edittext not editable in xml.And for clickable event you can use setOnTouchListener() event to do the same thing as like..

使edittext在xml中不可编辑。对于可单击事件,您可以使用setOnTouchListener()事件来执行相同的操作。

editText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
                        //do your stuff here..
        return false;
    }
});

#4


3  

android:editable is deprecated but you can do stg like below:

不赞成可编辑,但是你可以做如下的stg:

android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"

I used the cade above for edittexts which i use pickers to set data in. You can use input layout with this.

我使用上面的cade进行编辑,我使用拾音器来设置数据。您可以使用这个输入布局。

#5


0  

For me none of the answers resulted in a completely working solution.

对我来说,没有一个答案能产生一个完全有效的解决方案。

Showcase of my fix https://www.youtube.com/watch?v=oUA4Cuxfdqc

我的fix https://www.youtube.com/watch?v=oUA4Cuxfdqc的展示

Guide (noob-friendly), please note the few comments in the code.

指南(nobo -friendly),请注意代码中的一些注释。

Create a Java class for a custom EditText called EditTextDispatched.java

为一个名为edittextdispatch. Java的自定义编辑器创建一个Java类。

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * @author Martin Pfeffer
 * @see <a href="https://celox.io">https://celox.io</a>
 */
public class EditTextDispatched extends android.support.v7.widget.AppCompatEditText {

    private static final String TAG = "DispatchingEditText";

    private boolean editable = true;

    public EditTextDispatched(Context context) {
        super(context);
    }

    public EditTextDispatched(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextDispatched(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // get the current state
    public boolean isEditable() {
        return editable;
    }

    // set your desired behaviour
    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {

        if (editable) {
            // default behaviour of an EditText
            super.dispatchTouchEvent(motionEvent);
            return true;
        }

        // achieve the click-behaviour of a TextView (it's cuztom)
        ((RelativeLayout) getParent()).onTouchEvent(motionEvent);
        return true;
    }

}

Reference the EditTextDispatched in your xml (you will have to alter the pgk-name):

在您的xml中引用edittextsent(您必须更改pgk-name):

  <io.celox.brutus.EditTextDispatched
    android:id="@+id/row_field_value"
    style="@style/row_field_text_display"
    android:layout_alignParentBottom="true"
    android:text="@string/sample_field_value" />

To invoke:

调用:

private void changeEditTextBehaviour(EditTextDispatched value, boolean editable) {
    value.setEditable(editable);
    value.setEnabled(editable);
}

#6


0  

edittext enable and disable for the edit like this


EditText edit = (EditText) findviewby(R.id.edittext);

through java
edit.setEnabled(false); //non editable
edit.setEnabled(true); //editable

through xml
          <EditText
                android:id="@+id/otp_mpin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:editable="false"
                android:focusable="false"/>

#7


0  

Maybe you can try

也许你可以试试

editText.setEnabled(false);

#8


-1  

you can also do android:inputMethod="@null" in your EditText and it will not show the cursor or the keyboard and then you can easily grasp the click through setting listener.

你也可以使用android:inputMethod="@null"在你的EditText中,它不会显示光标或键盘,然后你可以轻松地通过设置监听器来抓取点击。