如何使编辑文本不可编辑,但在 JAVA 中可点击

是否有可能使 EditText可点击但不可编辑。

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

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

对于上面的要求,XML 中的解决方案是 android:editable="false",但是我想在 Java 中使用它。

但是,

如果我使用:-

et.setEnabled(false);

或者

 et.setKeyListener(null);

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

90021 次浏览

The trick here is :-

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

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.setOnTouchListener(new OnTouchListener() {


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

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

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

or by using below code in XML file for EditText.

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

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

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.

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.

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

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

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

Create a Java class for a custom EditText called EditTextDispatched.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):

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

Edittext enable and disable for the edit like this

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

Through java code

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"/>

Maybe you can try

editText.setEnabled(false);

I know this question has already been answered but to give a more current (i.e. 2018) answer here is the setup I use to achieve the requested (i.e. non-input or editable) effect:

    val etUserLoc: EditText = themedEditText(R.style.Pale_Blue_Text_Bold_13) {
id = R.id.et_comp_user_keys_var_user_location
inputType = InputType.TYPE_NULL
isClickable = true
isFocusable = false
textColor = resources.getColor(R.color.magenta)  // overrides the default theme color
text = Editable.Factory.getInstance().newEditable(resources.getString(R.string.loc_swakopmund))
}.lparams(width = matchConstraint, height = matchParent) {   }

Yes, this is ANKO since I no longer use XML in any of my Android Projects. The 'inputType' takes care of the 'isEditable' deprecation.

As editable is deprecated. you can use android:inputType as none in designing xml or as InputType.TYPE_NULL in coding.

Designing XML

<android.support.v7.widget.AppCompatEditText
android:id="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:inputType="none"
android:text="EditText"/>

Coding

edt.setClickable(true);
edt.setFocusable(false);
edt.setInputType(InputType.TYPE_NULL);

And below is the click event of edittext

edt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(YourActivity.this,"edt click", Toast.LENGTH_LONG).show();
}
});

As of 2020, if you also care about accessibility, you should avoid setting et.setFocusable(false); since that will make the EditText not reachable using Keyboard navigation.

Here is my solution:

Kotlin

editText.isFocusableInTouchMode = false


editText.isLongClickable = false //this will prevent copy and paste


editText.setOnClickListener {
// handle the click
// this will automatically make the edittext clickable = true
}

I used this to disable user input and show soft keyboard at the same time.

textView.apply {
filters = arrayOf(InputFilter { _, _, _, _, _, _ -> "" })
setOnLongClickListener { true }
isCursorVisible = false
}