【问题标题】:Autocompletetextview show keyboard doesn't workAutocompletetextview 显示键盘不起作用
【发布时间】: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


    【解决方案1】:

    在 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"
    

    【讨论】:

      【解决方案2】:

      你需要像这样请求焦点

      AutoCompleteTextView yourView = findViewById(R.id.your_id);
      yourView.requestFocus();
      

      【讨论】:

        【解决方案3】:

        调用requestFocus(); 为AutoCompleteTextView 编码喜欢,

        autocomplete.requestFocus();
        

        或者你也可以在xml中设置。

        【讨论】:

          猜你喜欢
          • 2022-10-24
          • 2021-02-01
          • 1970-01-01
          • 2011-03-07
          • 1970-01-01
          • 2011-07-28
          • 1970-01-01
          • 1970-01-01
          • 2017-04-13
          相关资源
          最近更新 更多