Listview with edittext - auto scroll on quot;nextquot;(带有edittext的列表视图-在“下一个上自动滚动)
问题描述
我有一个 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的列表视图-在“下一个"上自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有edittext的列表视图-在“下一个"上自动滚


- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01