问题
I have an AutoCompleteTextView which as usual provides suggestions after a user types 3 letters. I want once once I touch the suggestion list to hide the soft keyboard. What I have done below with the table layout hides the keyboard only when clicking anywhere but the suggestion list.
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 for custom list
<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>
回答1:
Use OnItemClickListener
. Hope this works :)
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);
}
});
UPDATED
Use this
in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
instead of -
in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
回答2:
As chakri Reddy Suggested:
It's working for me I just replaced
this:
in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
with:
in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
回答3:
1) Change AutoCompleteTextView dropdown height to MATCH_PARENT
setDropDownHeight(LinearLayout.LayoutParams.MATCH_PARENT);
2) Dismiss soft keyboard by overriding getView() of AutoCompleteTextView adapter
@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;
}
回答4:
Found this chunk of code in a similar thread. Hope it helps:
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 to the original post. Note that it is not the accepted answer.
回答5:
In case this helps anyone, I had a similar situation.
My AutoCompleteTextView was displayed on a DialogFragment with no Title Bar because I used getWindow().requestFeature(Window.FEATURE_NO_TITLE);
in onCreateDialog()
I added the following line after FEATURE_NO_TITLE
and the results list displayed over the keyboard:
@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;
}
回答6:
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);
}
}
});
回答7:
Try this guys..It will work..!!!
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);
} });
回答8:
I used the TextWatcher already to detect when the user has typed in at least 3 letters (which triggers a download API) as follows:
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) { }
});
来源:https://stackoverflow.com/questions/14679264/android-autocompletetextview-hide-soft-keyboard