在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。

在代码中,我设置android:enabled="false"。

虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。

我应该设置什么来禁用EditText?


当前回答

你可以使用android:focusable="false",但也需要禁用光标否则 复制/粘贴功能仍然可以工作。

因此,使用

android:focusable="false"
android:cursorVisible="false"

其他回答

简单:

editText.setEnabled(false);

使用TextView代替。

我使用谷歌新发布的材料设计库。在我的情况下,它工作时,我使用 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>

试试这个,对我来说很管用:

public class CustomEdittext extends EditText {

Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onCheckIsTextEditor() {
    // TODO Auto-generated method stub
    return mIsTextEditor;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mIsTextEditor=false;
    Boolean mOnTouchEvent=super.onTouchEvent(event);
    mIsTextEditor=true;     
    return mOnTouchEvent;
} }

注意:你需要添加this.getWindow().setSoftInputMode(windowmanager . layoutparam . soft_input_state_always_hidden); 在您的活动上,否则键盘将在第一时间弹出。

如果你使用android:editable="false", eclipse会提醒你这条消息“android:editable已弃用:请使用inputType”。

所以,我使用android:focusable="false"代替,它为我工作得很好。