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

时间:2012-12-30 12:31:11

标签: android android-layout android-edittext

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

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

实际上我想将编辑文本简单地用作带有提示的图像(无法更改)。我知道实际的方法是使用ImageViewTextView,但我希望它尝试使用EditText,因为我将只使用一个视图而不是2个。事情是动态的,所以没有XML。

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

但是,

如果我使用: -

et.setEnabled(false);

 et.setKeyListener(null);

它使EditText无法编辑,但同时也使其无法点击。

11 个答案:

答案 0 :(得分:133)

这里的诀窍是: -

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

答案 1 :(得分:17)

您可以使用java代码设置EditText: -

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

或在EditText的XML文件中使用以下代码。

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

答案 2 :(得分:7)

你可以使用它..

android:focusableInTouchMode="false"

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

答案 3 :(得分:6)

不推荐使用String。您可以在设计xml时将editable用作 none 或在编码中用作 InputType.TYPE_NULL

  

设计XML

android:inputType
  

编码

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

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

的点击事件
edittext

答案 4 :(得分:4)

不推荐使用

android:editable,但您可以执行以下操作:

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

我使用上面的cade进行edittexts,我使用拣货员设置数据。你可以使用输入布局。

答案 5 :(得分:2)

Edittext启用和禁用编辑功能

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

通过java代码

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

通过xml

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

答案 6 :(得分:1)

也许你可以试试

editText.setEnabled(false);

答案 7 :(得分:0)

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

展示我的修补程序https://www.youtube.com/watch?v=oUA4Cuxfdqc

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

为名为 EditTextDispatched.java 的自定义EditText创建一个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;
    }

}

引用xml中的EditTextDispatched(您必须更改pgk名称):

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

要调用:

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

答案 8 :(得分:0)

我知道这个问题已经得到解答,但是要给出一个最新的答案(即2018年),以下是我用来达到要求的效果(即非输入或可编辑)的设置:

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

是的,这是ANKO,因为我不再在任何Android项目中使用XML。 “ inputType”负责“ isEditable”的弃用。

答案 9 :(得分:0)

从2020年开始,如果您还关心可访问性,则应避免设置et.setFocusable(false);,因为那样会使使用键盘导航无法访问EditText。

这是我的解决方案:

科特琳

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
}

答案 10 :(得分:-1)

您也可以在public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('login'); } } $url = parse_url($request->url()); print_r($url["fragment"]); //return $next($request); } 中执行android:inputMethod="@null",它不会显示光标或键盘,然后您可以轻松掌握点击设置监听器。