我有一个AutoCompleteTextView,它通常在用户输入3个字母后提供建议。一旦我触摸建议列表隐藏软键盘,我想要一次。我在下面使用表格布局所做的只有在点击建议列表时才会隐藏键盘。
XML
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<AutoCompleteTextView
android:id="@+id/auto_insert_meds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textVisiblePassword|textMultiLine"
android:scrollHorizontally="false"
android:text=""
android:textSize="16sp" />
</TableRow>
Java的
TableLayout tbl = (TableLayout) findViewById(R.id.main_table);
tbl.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
});
用于自定义列表的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/medlist_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/med_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:padding="3dp"
android:textColor="@android:color/white" />
</LinearLayout>
使用OnItemClickListener
。希望这工作:)
AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);
text.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
}
});
更新
用这个
in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
代替 -
in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
正如chakri Reddy建议:
这对我有用,我刚刚更换了
这个:
in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
有:
in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
1)将AutoCompleteTextView下拉高度更改为MATCH_PARENT
setDropDownHeight(LinearLayout.LayoutParams.MATCH_PARENT);
2)通过覆盖AutoCompleteTextView适配器的getView()来关闭软键盘
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
typeTextView.getWindowToken(), 0);
}
return false;
}
});
return v;
}
在类似的线程中找到了这一块代码。希望能帮助到你:
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Link到原帖。请注意,这不是公认的答案。
如果这有助于任何人,我有类似的情况。
我的AutoCompleteTextView显示在没有标题栏的DialogFragment上,因为我在getWindow().requestFeature(Window.FEATURE_NO_TITLE);
中使用了onCreateDialog()
我在FEATURE_NO_TITLE
之后添加了以下行,并在键盘上显示结果列表:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialogo=super.onCreateDialog(savedInstanceState);
dialogo.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialogo.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return dialogo;
}
AutoCompleteTextView autoText= (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);
autoText.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
试试这个家伙..它会工作.. !!!
AutoCompleteTextView auto_text = (AutoCompleteTextView)findViewById(R.id.auto_insert_meds);
auto_text.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
} });
我已经使用TextWatcher检测用户何时输入了至少3个字母(触发下载API),如下所示:
searchAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// when the user has clicked on an item in the list, it will trigger onTextChanged.
// To avoid querying the server and showing the dropdown again, use searchAutoComplete.isPerformingCompletion()
if (!searchAutoComplete.isPerformingCompletion()){
if (s != null && s.length() >= 3) {
downloadSearchResults(s.toString());
} else {
searchAutoComplete.dismissDropDown();// hide dropdown after user has deleted characters and there's less than 3 visible
}
} else {
// user has clicked on a list item so hide the soft keyboard
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (in != null) in.hideSoftInputFromWindow(searchAutoComplete.getApplicationWindowToken(), 0);
}
}
@Override
public void afterTextChanged(Editable s) { }
});