谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是

已弃用;而且 但是没有成功。


当前回答

我试过以下几种方法:

codeEditText.setInputType(InputType.TYPE_NULL);

this.codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

  @Override
  public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {

      pickCode();

    }

  }

});
this.codeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickCode();

  }

});

但问题是,如果编辑文本是表单中的第一个,那么 它获得焦点和pickCode()代码,用于启动一个新活动 直接调用。所以我修改代码如下,它似乎 工作得很好(除了我不能将焦点设置在文本编辑上,但是 我不需要):

itemCodeEditText.setFocusable(false);

this.itemCodeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickItem();

  }

});

最好的问候,

我们欢迎评论,

约翰Goche

其他回答

他们弃用了“可编辑的”,但没有在inputType中提供相应的工作。

顺便说一下,inputType="none"有什么影响吗?在我看来,用不用它没有任何区别。

例如,默认的editText是单行的。但是您必须选择一个inputType,使其为单行。如果你选择“none”,它仍然是多行。

我试着这样做:

textView.setInputType( InputType.TYPE_NULL );

这应该有用,但对我来说没用。

我完成了下面的代码:

textView.setKeyListener(new NumberKeyListener() {
    public int getInputType() {
        return InputType.TYPE_NULL;
    }

    protected char[] getAcceptedChars() {
        return new char[] {};
    }
});

这很完美。

我使用EditText.setFocusable(false),如果我想编辑,再次设置为true。

我也遇到过同样的问题,但经过观察,我发现android:editable="false"只有当inputtype (android: inputtype ="ANY_VALUE")没有在Edittext中给出时才会工作。

从EditText中删除inputType并使用editable = false,其工作正常。

这个组合对我很有效:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>