I have an autocompletetextview which I have linked it to a webservice so it shows me suggestions as I type. Now how can I hide the soft keyboard when the user starts scrolling through the autocomplete dropdown? I looked through the net but didnt find any method to detech touches on the autocomplete dropdown.
问问题
7432 次
4 回答
8
我可以为此提出的最佳解决方案是当用户开始滚动列表并再次显示键盘时隐藏键盘,如果用户再次触摸 textview。这几乎适用于大多数操作系统版本和设备,与您可以看到的其他解决方案不同,例如设置 dropDownHeight 的高度。
下面是当用户开始滚动时隐藏键盘的示例代码。基本上,您需要在 AutoCompleteTextView 的适配器中创建一个触摸侦听器。
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(viewResourceId, parent, false);
holder = new ViewHolder();
init(convertView, holder);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
searchView.getWindowToken(), 0);
}
return false;
}
});
setView(position, holder);
return convertView;
}
于 2014-05-06T06:31:22.727 回答
2
我会将此答案或@ayorhan 的答案作为已接受的答案,这确实是在滚动下拉选择时处理关闭键盘的最佳方法。
这是@ayorhan 的解决方案,用于与 SimpleCursorAdapter 一起使用。我必须制作一个自定义 SimpleCursorAdapter 类:
public class SimpCursAdap extends SimpleCursorAdapter {
public SimpCursAdap(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
view.getApplicationWindowToken(), 0);
}
return false;
}
});
return view;
}
}
然后你可以在任何地方实例化这个类:
final SimpleCursorAdapter adapter = new SimpCursAdap(aContext,
aRowLayout,
null,
aColNames,
aRowViewsIds,
0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setStringConversionColumn(aValueColId);
autocompletetextview.setAdapter(adapter);
于 2017-01-20T18:28:12.503 回答
0
将此行添加到 XML 中对我来说很好
这将使键盘在滚动列表后面。
android:dropDownHeight="wrap_content"
于 2014-12-01T08:48:20.607 回答
0
如果我理解正确,您希望键盘消失,因为它会为您的下拉列表留出更多空间?也许这是相关的:
于 2013-05-11T09:14:53.137 回答