問題描述
我有一個 ListView,每行都有一個 EditText(除了幾個不可編輯的 TextView).當我在 EditText 中編輯文本時,軟鍵盤有下一步"按鈕 - 按下它會將焦點移動到下一個字段 - 這很棒.在最后一行,按鈕變為完成".
I have a ListView with one EditText on each row (in addition to a couple of non-editable TextView's). When I'm editing the text in the EditText, the soft keyboard has "Next" button - and pressing it moves the focus to the next field - this is great. On the last row, the button changes to "Done".
我正在使用 EditText.setImeOptions
根據是否為最后一行將按鈕設置為完成"或下一步".
I'm using EditText.setImeOptions
to set the button to "Done" or "Next" based on whether this is the last row or not.
問題是列表視圖可以有更多的行可以適應屏幕.發生這種情況時,在下一個可見行上按下一步"會將焦點再次移動到第一行.如何讓它滾動列表并轉到下一行?
The problem is that the listview can have more rows that can fit on the screen. When that happens, pressing "Next" on the next visible row moves the focus onto the first row again. How can I make it scroll the list and go to the next row instead?
作為參考,這是我在適配器中所做的:
For reference, here's what I'm doing in my adapter:
public class AuditAdapter extends BaseAdapter {
private Context context;
private int layoutResourceId;
private Audit audit;
...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final AuditItemHolder holder = (row == null ? new AuditItemHolder() : (AuditItemHolder)row.getTag());
if(row == null)
{
LayoutInflater inflater = ...;
row = inflater.inflate(layoutResourceId, parent, false);
...
holder.qtyf = (EditText)row.findViewById(R.id.item_quantity);
}
AuditItem item = audit.getItemAt(position);
holder.qtyf.setText("" + item.getQuantity());
holder.qtyf.setImeOptions(position == audit.size() - 1 ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT);
...
row.setTag(holder);
return row;
}
private static class AuditItemHolder {
...
EditText qtyf;
}
}
推薦答案
好的,經過長時間的努力,我終于找到了適合我的情況的 hack(不是正確的解決方案).在我的適配器的 getView
中,我添加了 onEditorActionListener
并在其中:
Ok, after struggling for a long time, I finally found a hack (not a proper solution) that works for my case. In the getView
of my adapter, I add the onEditorActionListener
and inside it:
ediField.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
ListView lv = (ListView)parent;
if(actionId == EditorInfo.IME_ACTION_NEXT &&
lv != null &&
position >= lv.getLastVisiblePosition() &&
position != audit.size() - 1) { //audit object holds the data for the adapter
lv.smoothScrollToPosition(position + 1);
lv.postDelayed(new Runnable() {
public void run() {
TextView nextField = (TextView)holderf.qtyf.focusSearch(View.FOCUS_DOWN);
if(nextField != null) {
nextField.requestFocus();
}
}
}, 200);
return true;
}
return false;
}
});
這篇關于帶有edittext的列表視圖-在“下一個"上自動滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!