Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中禁用EditText_Android_Android Edittext_Disabled Input - Fatal编程技术网

如何在Android中禁用EditText

如何在Android中禁用EditText,android,android-edittext,disabled-input,Android,Android Edittext,Disabled Input,如何在Android中禁用EditText字段中的键入?假设EditText是您的EditText对象: editText.setEnabled(false); 代码: editText.setEnabled(false); 或者,在XML中: android:editable=“false” 要在android中禁用编辑文本: editText.setEnabled(false); 如果要显示edittext但不输入任何值,则可以编写edittext.setInputType(0),下面

如何在Android中禁用
EditText
字段中的键入?

假设
EditText
是您的
EditText
对象:

editText.setEnabled(false);
代码:

editText.setEnabled(false);
或者,在XML中:

android:editable=“false”
要在android中禁用编辑文本:

editText.setEnabled(false);

如果要显示edittext但不输入任何值,则可以编写edittext.setInputType(0),下面的代码在android中禁用edittext

editText.setEnabled(false);

我认为这是安卓系统中的一个bug。可以通过添加此补丁来修复:)
检查这些链接 和


希望它会有用。

您可以使用
EditText.setFocusables(false)
禁用编辑
EditText.setFocusableInTouchMode(true)
启用编辑

将此内容写入父级:

edittext.setFocusable(false); 
edittext.setEnabled(false);

InputMethodManager imm = (InputMethodManager)
  getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
  //removes on screen keyboard
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
例如:

<RelativeLayout 
android:id="@+id/menu_2_zawezanie_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_2_horizontalScrollView"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:background="@drawable/rame">

<EditText
    android:id="@+id/menu2_et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/menu2_ibtn"
    android:gravity="center"
    android:hint="@string/filtruj" >
</EditText>
<ImageButton
    android:id="@+id/menu2_ibtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:src="@drawable/selector_btn" />


根据我的说法……如果您已经在XML文件中声明了
edittext
,并在XML文件中设置了属性
“android:enabled=false”
,并在运行时在java文件中禁用该属性,则只需添加2或3行

  • 首先创建对象
  • 声明EditText et1
  • 通过身份证

    et1=(EditText)FindViewById(R.id.edittext1)
    et1.setEnabled(false)


  • 您可以通过java文件在运行时启用或禁用每个控件,通过XML文件在设计时启用或禁用每个控件。

    只需将edittext的focusable属性设置为“false”,就可以了

    <EditText
            android:id="@+id/EditTextInput"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:gravity="right"
            android:cursorVisible="true">
        </EditText>
    
    
    
    下面的选项不起作用

    代码:

    editText.setEnabled(false)
    或者,在XML中:
    
    android:editable=“false”

    您可以尝试以下方法:

    private void disableEditText(EditText EditText){
    editText.setFocusable(false);
    editText.setEnabled(false);
    editText.setCursorVisible(false);
    editText.setKeyListener(空);
    editText.setBackgroundColor(Color.TRANSPARENT);
    }
    
    已启用的编辑文本:

    禁用的编辑文本:

    它对我很有用,希望对您有所帮助。

    启用:

    Edittext edittext = (EditText)findViewById(R.id.edit);
    edittext.setEnabled(false);
    
    private void enableEditText() {
        mEditText.setFocusableInTouchMode(true);
        mEditText.setFocusable(true);
        mEditText.setEnabled(true);
    }
    
    禁用:

    private void disableEditText() {
        mEditText.setEnabled(false);
        mEditText.setFocusable(false);
        mEditText.setFocusableInTouchMode(false);
    }
    

    XML
    editable
    属性已弃用,因为

    您现在应该使用值为
    none
    inputType
    属性。 但是有一个bug使得这个功能毫无用处


    您可以查看问题状态。

    右键单击文本区域并取消选中可编辑选项:


    在EditText下的layout.xml文件中将
    inputType
    属性设置为none

    尝试此Kotlin代码


    editField.isEnabled=!editField.isEnabled

    在EditText中输入时禁用键盘?有两个EditText字段。第一个用于输入电子邮件。如果电子邮件有效,则将启用第二个EditText字段。但问题是,当键入无效电子邮件并单击软键盘上的“下一步”按钮时,它会转到第二个文本字段并可以输入数据即使我已使edittext.setEnabled(false)生效。我正在使用android 2.3.1来帮助那些希望在此处看到代码而不是使用超链接的人:
    edittext.setEnabled(false)
    ,与
    editText.setFocusable(false)结合使用
    将实现这一点。这同样适用于XML:
    android:enabled=“false”
    android:focusable=“false”
    。谢谢大家!只需将edittext的focusable属性设置为“false”,就完成了。这是我的问题。我假设在
    setFocusable(false)
    之后,我可以翻转setFocusable(true)。。。事实并非如此。设置
    setFocusableInTouchMode(true)
    对我起到了作用。答案很好,但可以通过删除更改背景颜色的行并添加editText.setKeyListener(null)来改进;欢迎来到Stack Overflow,Nuwan Bandara。您的答案与JTextArea相关,而不是来自Android框架的EditText。您不太可能提供此问题的答案。