自动完成文本视图显示键盘无法正常工作

7
我正在尝试使用片段构建Android应用程序,其中包含Autocompletetextview。我希望在显示片段时键盘出现。我尝试了一些方法,但没有成功。
这是我的代码:
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);

    }
}

谢谢您的阅读和帮助, 祝您有美好的一天!
3个回答

3

在 XML 代码中使用以下内容:

 <AutoCompleteTextView
    android:id="@+id/editArea"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="right"
    android:singleLine="true"
    android:textColor="@android:color/primary_text_light" >

    <requestFocus />
  </AutoCompleteTextView>

我在清单文件中的活动中添加了以下内容:

android:windowSoftInputMode="stateAlwaysVisible"

1
你需要像这样请求焦点。
AutoCompleteTextView yourView = findViewById(R.id.your_id);
yourView.requestFocus();

1

通过编程调用requestFocus();来为AutoCompleteTextView设置焦点,例如:

autocomplete.requestFocus();

或者您也可以在XML中设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接