【发布时间】:2021-11-22 06:57:46
【问题描述】:
我正在使用带有 json 的 WordPress 网站制作应用程序,我已将 searchView 添加到我的 MainActivity 中,一切正常,但唯一的问题是当 MainActivity 显示时,searchView 的键盘会自动出现,我希望它在用户点击时显示搜索栏..
这是我显示 searchView 的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgcoor"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.punjabidharti.punjabistatus.MainActivity"
tools:showIn="@layout/activity_main">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search Status Here"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
android:iconifiedByDefault="false"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_weight="1"
android:layout_below="@+id/searchView"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="60dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/hash"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:text="#Hashtag"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#fff"
android:background="@color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是使它工作的代码:
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
SearchView searchview = (SearchView) findViewById(R.id.searchView);
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String s) {
// some code here
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
请帮忙..
【问题讨论】:
-
android:windowSoftInputMode="stateHidden"将此添加到 AndroidManifest.xml 文件中的 MainAcitivity 中 -
... 根据您的用例,您可能需要
android:windowSoftInputMode="stateHidden|adjustResize"以允许Android 调整活动大小,使其不被键盘覆盖(如有疑问check the docs)跨度> -
不起作用兄弟 :( @Nitish
-
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);尝试在运行时在 onCreate() 方法中添加它,如果仍然无法正常工作,请更新 Manifest 文件的代码 ` -
那么你将不得不提供更多信息,这是正确的方法,如果键盘仍然弹出,那么也许分享你所有的布局,以及你的活动/片段代码.
标签: android json android-studio search searchview