谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
当前回答
试试这段代码。它在我的项目中工作,所以它将在你的项目中工作。
android:editable="false"
其他回答
当我想要一个活动不关注EditText,也不显示键盘时,点击,这是为我工作。
向主布局添加属性
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
然后是EditText字段
android:focusable="false"
android:focusableInTouchMode="false"
这里有一个例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_home"
android:background="@drawable/bg_landing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.woppi.woppi.samplelapp.HomeActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/fromEditText"
android:layout_weight="1"
android:hint="@string/placeholder_choose_language"
android:textColor="@android:color/white"
android:textColorHint="@android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="onSelectFromLanguage" />
</RelativeLayout>
只需使用android:enabled="false"即可。这也会给EditText一个外观,使它看起来不可编辑。
如果你想在java代码中这样做,只需使用这一行来禁用它:
editText.setEnabled(false);
这是为了启用它:
editText.setEnabled(true);
这样做的工作为我,我可以选择和复制到剪贴板,但我不能修改或粘贴。
android:enable="true"
android:textIsSelectable="true"
android:inputType="none"
将此添加到您的EditText xml文件:
<EditText ...
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false">
</EditText>
它的效果与android:editable="false"相同。对我有用,希望对你也有用。