【问题标题】:Make EditText editable programmatically以编程方式使 EditText 可编辑
【发布时间】:2016-03-08 14:09:16
【问题描述】:

我有一个带有一个 textView 和一个 EditText 和一个按钮的布局。 EditText 在 xml 文件中设置为不可编辑。我想通过代码启用它。

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/textView12"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
         />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etName"
        android:layout_below="@+id/textView12"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:editable="false"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit"
        android:onClick="edit"
        android:id="@+id/btnName"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

【问题讨论】:

标签: android android-edittext


【解决方案1】:

从 android xml 文件中删除 android:editable="false" 这一行,因为该方法在 android 中已被弃用。并使用下面的代码来做到这一点。

  • 使用 java 代码使 EditText 不可编辑

onCreate 的活动中,在setContentView() 方法之后执行以下操作

EditText text = (EditText) findViewById(R.id.etName);
text.setTag(text.getKeyListener());
text.setKeyListener(null);
  • 并使用以下代码使其可编辑

    text.setKeyListener((KeyListener) textView.getTag());
    

希望对你有所帮助。

【讨论】:

  • 什么是崩溃的堆栈跟踪??
【解决方案2】:

嘿,而不是可编辑使用android:enabled="false" 和代码setEnabled(true)

【讨论】:

  • 反馈意见告诉我
  • 您好 Naveen,感谢您的宝贵建议。但是你的建议不会按照我的要求工作。我希望edittext最初不可编辑,单击Imagebutton时需要将其变为可编辑。根据您的建议,最初它可以在加载时进行编辑。
  • 试试这个组合一次,android:enabled="false" android:focusable="false" 和代码中的setEnabled(true)
猜你喜欢
  • 1970-01-01
  • 2011-01-16
  • 2021-09-30
  • 2020-10-16
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
相关资源
最近更新 更多