【发布时间】:2014-05-06 10:13:01
【问题描述】:
我正在尝试使用包含 Autocompletetextview 的片段构建 android 应用程序 ,我希望在显示片段时显示键盘。 我已经尝试了一些东西,但没有成功。
这是我的代码
xml
<AutoCompleteTextView
android:id="@+id/editArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:singleLine="true"
android:textColor="@android:color/primary_text_light"
android:gravity="right" >
</AutoCompleteTextView>
我的 Java 片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLayout = inflater.inflate(R.layout.change_area, null, false);
area = (AutoCompleteTextView) mLayout.findViewById(R.id.editArea);
showHideKeyboard(true);
citiesList = singleToneCitiesList.getInstance().getList();
mAdapter = new CitiesAdapter(getActivity(),
R.layout.autocommplete_text, citiesList);
area.setAdapter(mAdapter);
area.setThreshold(1);
area.setOnItemClickListener(this);
return mLayout;
}
private void showHideKeyboard(boolean showHide) {
if (showHide) {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
area.requestLayout();
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInputFromWindow(getActivity()
.getCurrentFocus().getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
} else {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(area.getWindowToken(), 0);
}
}
感谢您阅读并帮助我解决此问题, 祝你今天过得愉快!
【问题讨论】:
标签: java android android-fragments