There is a table

TableLayout table 

Filled TableRow.

There is a processing of pressing a physical USB keyboard, the code is as follows:

 TableRow row = new TableRow(context); ... row.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(event!=null && event.getAction()==KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_DOWN: case KeyEvent.KEYCODE_DPAD_UP: TableRow fromRow = (TableRow) v; int fromRowIndex = table.indexOfChild(fromRow); fromRow.requestFocus(); CharSequence cellText = ((TextView) fromRow.getChildAt(2)).getText(); editText.setText(cellText); //Далее меняем цвета у текущей строки и предыдущей выделенной строки //предыдущая строка if (lastSelectedTableItem != -1) { TableRow prevRow = ((TableRow) table.getChildAt(lastSelectedTableItem)); updateRowColor(prevRow, lastSelectedRowStatus); for (int i = 0; i < prevRow.getChildCount(); ++i) { TextView tv = (TextView) prevRow.getChildAt(i); tv.setTextColor(Color.BLACK); } } lastSelectedRowStatus = finalStatus; lastSelectedTableItem = fromRowIndex; //выбранная строка fromRow.setBackgroundColor(((TextView) fromRow.getChildAt(0)).getCurrentTextColor()); for (int i = 0; i < fromRow.getChildCount(); ++i) { TextView tv = (TextView) fromRow.getChildAt(i); int colorCode; if (finalStatus) { if (Build.VERSION.SDK_INT >= 23) colorCode = getColor(R.color.colorSelectedCell); else colorCode = getResources().getColor(R.color.colorSelectedCell); } else colorCode = Color.WHITE; tv.setTextColor(colorCode); } break; case KeyEvent.KEYCODE_ENTER: editText.setSelection(editText.getText().length()); editText.requestFocus(); break; } } return false; } }); 

Now I have a problem with the following:

When pressed, first, let's say, the Down key (KEYCODE_DPAD_DOWN) (respectively, the next lower row of the table is highlighted), and right after it - the Up key (KEYCODE_DPAD_UP) —the lower row is highlighted again, not the top row, as expected.

(On the contrary - first press Up, then Down - likewise - the previous upper line is highlighted, not the lower one).

Tracking debugging that I'm doing wrong is unrealistic because of the connected keyboard.

    1 answer 1

    After the case KeyEvent.KEYCODE_DPAD_DOWN: should go break; otherwise, when you press down, you will have the code written for the up button.

    By the way, the computer and the device can be connected via wi-fi in Android Studio, if you have the opportunity)