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

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

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

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


当前回答

要禁用EditText的功能,只需使用:

EditText.setInputType(InputType.TYPE_NULL);

如果你想以某种方式启用它,那么你可以使用:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);

其他回答

我相信正确的方法是设置android:editable="false"。

如果你想知道为什么我的链接指向TextView的属性,你的答案是,因为EditText从TextView继承:

EditText是一个薄薄的贴面 配置自身为的TextView 可编辑的。

更新: 正如下面的评论中提到的,editable已弃用(从API级别3开始)。您应该使用inputType(值为none)。

正如一些回答提到的,如果你禁用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就不能被点击了。

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

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:focusable="false",但也需要禁用光标否则 复制/粘贴功能仍然可以工作。

因此,使用

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

使用android:editable="false"是Depracted。相反,你需要使用android:focusable="false"