ListView:如何将nextFocusDown设置为下一行中的元素?

时间:2014-03-09 11:52:12

标签: android listview android-listview

我有一个ListView,每行有两个EditText

<EditText
        android:id="@+id/trainingDetailCol1"
        android:layout_height="wrap_content"
        android:layout_width="0dip"
        android:layout_weight="0.3"
        android:gravity="center"
        android:singleLine="true"
        android:nextFocusDown="@+id/trainingDetailCol2"
        android:inputType="numberDecimal"
        android:digits="0123456789.,"/>
<EditText
        android:id="@id/trainingDetailCol2"
        android:layout_height="wrap_content"
        android:layout_width="0dip"
        android:layout_weight="0.3"
        android:gravity="center"
        android:singleLine="true"
        android:inputType="numberDecimal"
        android:digits="0123456789.,"/>

有没有办法关注trainingDetailCol1,但在下一行?所以它是EditText1-Row1 - &gt; EditText2-Row1 - &gt; EditText1-Row2等等。现在,通过向第二个android:nextFocus...添加EditText,焦点将跳转到第2行的EditText2

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我所做的是在EditText上设置OnKeyListener并监听KeyCode_Enter。如果它被点击了,我看着看了EditText应该收到焦点并给它。就我而言,它看起来像这样

int childRowCount = this.rootLayout.getChildCount(); for (int i = 0; i < childRowCount; i++) { LinearLayout rootLayout = (LinearLayout) this.rootView.findViewById(R.id.root); LinearLayout row = (LinearLayout) rootLayout.getChildAt(i); EditText et = (EditText) row.findViewById(R.id.some_et); et.setTag(i); // -> i save the row in which i am in a tag et.setOnKeyListener(this); }

在听众中我做到了 @Override public boolean onKey(View view, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { try { Integer tag = (Integer) view.getTag(); LinearLayout rowX = (LinearLayout) this.rootLayout.getChildAt(tag + 1); EditText nextET = (EditText)rowX.findViewById(R.id.next); fakeEditText.setFocusableInTouchMode(true); fakeEditText.requestFocus(); } catch (ClassCastException e) { // do nothing return false; } return true; } return false; }

答案 1 :(得分:0)

科特琳。

在ListViewAdapter类中,有趣的getView(),如下添加lastEditText.setOnKeyListner:

    lastEditText.setOnKeyListener { v, keyCode, event ->
    var setOnKeyListener = false
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
        try {
            val nextRow = getViewByPosition(position + 1, parent as ListView) as LinearLayout
            val nextET = nextRow.findViewById(R.id.firstEditText) as EditText
            nextET.isFocusableInTouchMode = true
            nextET.requestFocus()
        } catch (e: Exception) {
            // do nothing
        }
        setOnKeyListener = true
    }
    setOnKeyListener
}

在有趣的getView()之后添加有趣的getViewByPosition(),如下所示:

    private fun getViewByPosition(pos: Int, listView: ListView): View? {
    val firstListItemPosition: Int = listView.firstVisiblePosition
    val lastListItemPosition: Int = firstListItemPosition + listView.childCount - 1
    return if (pos < firstListItemPosition || pos > lastListItemPosition) {
        listView.adapter.getView(pos, null, listView)
    } else {
        val childIndex = pos + listView.headerViewsCount - firstListItemPosition
        listView.getChildAt(childIndex)
    }
}