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

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


当前回答

你可以通过以下方法在edittext和editable之间切换:

public void toggleEditableOfEditText(EditText editText){
        if (editText.getKeyListener() != null) {
            editText.setTag(editText.getKeyListener());
            editText.setKeyListener(null);
        } else {
            editText.setKeyListener((KeyListener) editText.getTag());
        }
    }

然后使用这个方法使edittext可编辑或不可编辑:

toggleEditableOfEditText(editText);

其他回答

<EditText
    android:id="@+id/txtDate"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="2dp"
    android:clickable="false"
    android:cursorVisible="false"
    android:gravity="center" />

并使用以下:

txtDate.setKeyListener(null);

将此添加到您的EditText xml文件:

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

它的效果与android:editable="false"相同。对我有用,希望对你也有用。

你可以使用android:editable="false",但我真的会建议你 使用setEnabled(false),因为它为用户提供了一个视觉线索 控件不能编辑。所有人都使用相同的视觉线索 禁用小部件和一致性很好。

试试这段代码。它在我的项目中工作,所以它将在你的项目中工作。

android:editable="false"

简短的回答: 我使用android:focusable="false",它工作。点击收听现在也起作用了。