【问题标题】:Inflated EditText View not selectable膨胀的 EditText 视图不可选择
【发布时间】:2016-07-28 09:50:28
【问题描述】:

正在膨胀的 XML 文件 (matrix_cell.xml)。

<EditText 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rect_edit_text"
    android:textCursorDrawable="@drawable/cursor_black"
    android:textColor="@color/colorAccent"
    android:gravity="center_horizontal"
    android:inputType="number"
    android:layout_margin="1dp"
    android:layout_weight="1" />

膨胀视图的代码:

LayoutInflater inflater = LayoutInflater.from(this);
TableRow tableRow = new TableRow(this);
inflater.inflate(R.layout.matrix_cell, tableRow, true);

创建的 EditText 视图不可选择,当单击 EditText 视图时,输入焦点仍保留在前一个输入焦点上。

【问题讨论】:

  • 可选择是什么意思?
  • 单击时光标不会显示在 editText 上。它保留在上一个焦点上的输入上。
  • 您是否在调试应用程序时尝试手动选择它?您是否尝试在代码中以编程方式选择它?你的代码的其余部分是什么样的? (xml, java)
  • 当你点击时,你得到键盘了吗?如果是这样,当你打字时会发生什么?数字是否出现在编辑文本中?

标签: android


【解决方案1】:

id 添加到您的EditText

可以这样实现

<EditText
    android:id="@+id/edittext"
    android:inputType="text"
    ...
    ...
/>

它应该可以解决您的问题。此外,您是否使用任何布局并将您的EditText 作为其子级?必须这样做。你必须记住EditText 是一个视图而不是Layout

inflator 为视图充气后,使用view.findViewById(R.id.edittext) 方法获取您的EditText 进行工作。

更新

正如Alexander Kulyakhtinthis answer 中所说,

for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
    View nextChild = ((ViewGroup)v).getChildAt(i);
}

你可以得到孩子。正如我所说,使用nextChild.findViewById(R.id.edittext) 获取EditText 并使用它。

【讨论】:

  • 谢谢。将 id 添加到 EditText 解决了这个问题。但我使用 EditText 作为 TableLayout 的单元格。那么id不会引用布局中的所有editTexts吗?难道我还需要使用 getChildAt() 来获取特定的 editText 吗?
  • 你正在使用充气机!所以不会影响!确保表格单元格是线性或相对布局,并且 edittext 是它们的子级。就像我在充气后所说的那样调用id。如果这解决了您的问题,请将答案标记为正确。谢谢。
【解决方案2】:

正如@Sibidharan 所说,在您的xml 中添加id

<EditText
    android:id="@+id/editText"
    ...
    ...
/>

在您的活动中,确保您已对其进行初始化。

EditText editText= (EditText)findViewById(R.id.editText)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2017-01-08
    • 2016-09-29
    • 2012-05-16
    相关资源
    最近更新 更多