問題描述
我有一個(gè) ListView,每行都有一個(gè) EditText(除了幾個(gè)不可編輯的 TextView).當(dāng)我在 EditText 中編輯文本時(shí),軟鍵盤有下一步"按鈕 - 按下它會(huì)將焦點(diǎn)移動(dòng)到下一個(gè)字段 - 這很棒.在最后一行,按鈕變?yōu)橥瓿?.
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
根據(jù)是否為最后一行將按鈕設(shè)置為完成"或下一步".
I'm using EditText.setImeOptions
to set the button to "Done" or "Next" based on whether this is the last row or not.
問題是列表視圖可以有更多的行可以適應(yīng)屏幕.發(fā)生這種情況時(shí),在下一個(gè)可見行上按下一步"會(huì)將焦點(diǎn)再次移動(dòng)到第一行.如何讓它滾動(dòng)列表并轉(zhuǎn)到下一行?
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;
}
}
推薦答案
好的,經(jīng)過長(zhǎng)時(shí)間的努力,我終于找到了適合我的情況的 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;
}
});
這篇關(guān)于帶有edittext的列表視圖-在“下一個(gè)"上自動(dòng)滾動(dòng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!