war*_*rna 111 android android-edittext
我想制作一个只读EditText视图.执行此代码的XML似乎是android:editable="false",但我想在代码中执行此操作.
我怎样才能做到这一点?
Nik*_*hil 126
请使用此代码..
Edittext.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)
小智 93
如果你setEnabled(false)然后你editText会看起来残疾(灰色等).您可能不想更改编辑器的可视方面.
一种不太干扰的方式是使用setFocusable(false).
我相信这可以更接近您的初始意图来回答您的问题.
Sal*_*lim 28
在XML中使用
android:editable="false"
Run Code Online (Sandbox Code Playgroud)
例:
<EditText
android:id="@+id/EditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false" />
Run Code Online (Sandbox Code Playgroud)
小智 20
这对我有用:
EditText.setKeyListener(null);
Run Code Online (Sandbox Code Playgroud)
Adr*_*man 12
editText.setInputType(InputType.TYPE_NULL);
Run Code Online (Sandbox Code Playgroud)
根据文档,这可以防止显示软键盘.它还可以防止粘贴,允许滚动并且不会改变视图的视觉方面.但是,这也会阻止在视图中选择和复制文本.
从我的测试设置setInputType到TYPE_NULL似乎在功能上等同于折旧android:editable="false".此外,android:inputType="none"似乎没有明显的效果.
android:editable =“ false”已弃用。因此,您不能使用它来使编辑文本变为只读。
我使用波纹管解决方案来完成此操作。我在这里用过
android:inputType =“ none”
android:textIsSelectable =“ true”
android:focusable =“ false”
试试看:)
<EditText
android:id="@+id/et_newsgpa_university"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_educational_institute"
android:textSize="@dimen/regular_text"
android:inputType="none"
android:textIsSelectable="true"
android:focusable="false"
android:maxLines="1"
android:imeOptions="actionNext"/>
Run Code Online (Sandbox Code Playgroud)
editText.setEnabled(false);
editText.setFilters(new InputFilter[] { new InputFilter() {
public CharSequence filter(CharSequence src, int start, int end,
Spanned dst, int dstart, int dend) {
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
} });
Run Code Online (Sandbox Code Playgroud)
这将为您提供不可编辑的EditText过滤器.首先需要将所需的文本放在editText字段上,然后应用此过滤器.
写这两行对你的工作来说已经足够了.
yourEditText.setKeyListener(null);
yourEditText.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161351 次 |
| 最近记录: |