2017-07-26 77 views
0

我的活动中有一个不属于工具栏的searchview小部件。在我的onCreate方法中,我使用了 searchView.setIconified(false); ,这确实使searchView成为焦点,但它不会提起键盘,除非我再次单击它。我如何让他们的键盘弹出?在Android中启动活动时不会以编程方式弹出Searchview键盘

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </android.support.v7.widget.Toolbar> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="15dp" 
     android:background="@color/lighterGrey"> 
    <android.support.v7.widget.SearchView 
     android:id="@+id/search_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:transitionName="@string/search_transition" 
     android:background="@drawable/search_shape"/> 
    </LinearLayout> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/acronym_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

的java:

searchView = (SearchView) findViewById(R.id.search_view); 
searchView.setIconified(false); 
searchView.setFocusable(true); 
searchView.setIconified(false); 
searchView.requestFocusFromTouch(); 
View view = this.getCurrentFocus(); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
if (imm != null) { 
    imm.showSoftInput(view, 0); 
} 

回答

0

你需要更新您的AndroidManifest.xml。将android:windowSoftInputMode="stateVisible|adjustPan"添加到您的活动声明中。例如:

<activity 
     android:name=".MyActivity" 
     android:windowSoftInputMode="stateVisible|adjustPan"> 
    </activity> 

stateVisible将尽快活动开始显示软件键盘。 adjustPan将调整视图以补偿显示的键盘(以便您尝试显示的内容不被键盘覆盖)。

+0

这并没有为我工作 – DessertsAndStuff

+0

[您是否尝试过编程显示键盘?](https://开头计算器.com/questions/1109022/close-hide-the-android-soft-keyboard?rq = 1)如果这不起作用,请发布代码和XML,以便我们可以帮助您解决问题 –

+0

是的,我也试过,没有工作 – DessertsAndStuff

0

尝试添加以下

searchView.setFocusable(真); searchView.setIconified(假); searchView.requestFocusFromTouch();

它应该重点打开键盘。 在某些设备上,你必须也增加似乎

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

if (imm != null) {
imm.showSoftInput(view, 0);
}

+0

不工作:(我要用我的代码更新问题 – DessertsAndStuff

相关问题