如何在ListView中请求关注下一个EditText?

时间:2018-10-13 20:45:26

标签: android android-studio listview adapter

我希望在单击Enter之后焦点会像图片中的一样变化。然后我单击从左进入的焦点将按需更改,但是当我单击从右进入的焦点仅在上下移动时。我不知道如何从下一行获取editTexts。我该如何改变呢?

我在XML中的ListView:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="0dp"
    android:divider="@null"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/textNamesOfTeams"
    android:layout_above="@+id/start"
    android:descendantFocusability="afterDescendants"/>

还有我的CustomAdapter:

 class CustomAdapter extends BaseAdapter {
    private Context context;
    private String[] rValues = {"R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24"};

    CustomAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return quantityOfTeam;
    }

    @Override
    public String getItem(int position) {
        return names[position] +"\n" + names[position + quantityOfTeam];
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.brazylian_item_list, null, true);

            names = new String[quantityOfTeam * 2];
            holder.editText = convertView.findViewById(R.id.brazItemListEditTextName1);
            holder.editText2 = convertView.findViewById(R.id.brazItemListEditTextName2);
            holder.textView = convertView.findViewById(R.id.brazItemListtextViewR);



            convertView.setTag(holder);
        } else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(rValues[position]);



        holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    names[position] = holder.editText.getText().toString();

                }
            }
        });

        holder.editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    names[position + quantityOfTeam] = holder.editText2.getText().toString();
                }
            }
        });

        return convertView;
    }

    private class ViewHolder {

        protected EditText editText;
        protected EditText editText2;
        protected TextView textView;

    }

}

列表项:

<TextView
    android:id="@+id/brazItemListtextViewR"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_toEndOf="@+id/brazItemListtextViewR"
    >
    <EditText
        android:id="@+id/brazItemListEditTextName1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="15sp"
        android:background="@drawable/rounded_edittext"
        android:layout_marginTop="5dp"
        android:maxLength="15"
        android:singleLine="true"
        android:nextFocusDown="@+id/brazItemListEditTextName2"/>

    <EditText
        android:id="@+id/brazItemListEditTextName2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="15sp"
        android:background="@drawable/rounded_edittext"
        android:layout_marginTop="5dp"
        android:maxLength="15"
        android:singleLine="true"
        />
</LinearLayout>

Move to another EditText when Soft Keyboard Next is clicked on Android我以这种方式感到疲倦,但是正如我之前所说的,在我可以改变焦点之前,我单击了从左到右的Enter键。我从下一行找不到ID到EditTexts。

2 个答案:

答案 0 :(得分:1)

在该行的最后一个Editextview中使用此代码。这对我有用

android:imeOptions="actionNext"
android:nextFocusDown="@+id/nextRowEdittextId"

答案 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)
    }
}