在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。
在代码中,我设置android:enabled="false"。
虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。
我应该设置什么来禁用EditText?
在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。
在代码中,我设置android:enabled="false"。
虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。
我应该设置什么来禁用EditText?
当前回答
简单:
editText.setEnabled(false);
其他回答
如果你使用android:editable="false", eclipse会提醒你这条消息“android:editable已弃用:请使用inputType”。
所以,我使用android:focusable="false"代替,它为我工作得很好。
您可以尝试以下方法:
private void disableEditText(EditText editText) {
editText.setFocusable(false);
editText.setEnabled(false);
editText.setCursorVisible(false);
editText.setKeyListener(null);
editText.setBackgroundColor(Color.TRANSPARENT);
}
启用EditText:
禁用EditText:
这对我有用,希望能帮到你。
正如一些回答提到的,如果你禁用editText,他变成灰色,如果你设置focusable false光标显示。
如果你只想用xml来做,这就可以了
<YourFloatLabel
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/view_ads_search_select"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"/>
</YourFloatLabel>
我只是添加了一个FrameLayout出现在editText上面,并将其设置为可聚焦和可点击,这样editText就不能被点击了。
在类中设置以下属性:
editText.setFocusable(false);
editText.setEnabled(false);
它会像你要求的那样顺利工作。
我使用谷歌新发布的材料设计库。在我的情况下,它工作时,我使用 android: focusable = " false " android: cursorVisible = " false "
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/to_time_input_layout"
app:endIconMode="custom"
app:endIconDrawable="@drawable/ic_clock"
app:endIconContentDescription="ToTime"
app:endIconTint="@color/colorAccent"
style="@style/OutlinedEditTextStyle"
android:hint="To Time">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/to_time_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false" />
</com.google.android.material.textfield.TextInputLayout>