Java startSearch不启动搜索活动
我在活动中添加了一个SearchView,并定义了一个搜索活动来处理搜索。我添加了OnQueryTextListener,并从Java startSearch不启动搜索活动,java,android,xml,searchview,searchactivity,Java,Android,Xml,Searchview,Searchactivity,我在活动中添加了一个SearchView,并定义了一个搜索活动来处理搜索。我添加了OnQueryTextListener,并从onQueryTextSubmit()方法调用了startSearch()方法。当我将全局搜索设置为true时,它会启动谷歌搜索。当我将其设置为false时,什么也没有发生。代码如下: final SearchView searchText = (SearchView) customView.findViewById(R.id.txtSearch); searc
onQueryTextSubmit()
方法调用了startSearch()
方法。当我将全局搜索设置为true
时,它会启动谷歌搜索。当我将其设置为false
时,什么也没有发生。代码如下:
final SearchView searchText = (SearchView) customView.findViewById(R.id.txtSearch);
searchText.setFocusable(true);
searchText.setIconifiedByDefault(false);
searchText.setQueryHint(SearchActivity.this.getResources().getString(R.string.search_text_hint));
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
final String searchText = newText;
if (newText.equals("")) {
suggestAdapter.removeAll();
suggestAdapter.notifyDataSetChanged();
return true;
}
if (newText.length() < 4) {
return true;
}
if (!last_onTextChanged.equals(newText))
onchangeHandler.removeCallbacksAndMessages(null);
else
return true;
last_onTextChanged = newText;
onchangeHandler.postDelayed(new Runnable() {
@Override
public void run() {
getSearchSuggestions(searchText);
}
},800);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
startSearch(query,true,null,false);
return true;
}
};
searchText.setOnQueryTextListener(queryTextListener);
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>
final SearchView searchText=(SearchView)customView.findviewbyd(R.id.txtSearch);
searchText.setFocusable(true);
searchText.setIconifiedByDefault(false);
searchText.setQueryHint(SearchActivity.this.getResources().getString(R.string.search_text_hint));
final SearchView.OnQueryTextListener queryTextListener=新建SearchView.OnQueryTextListener(){
@凌驾
公共布尔onQueryTextChange(字符串newText){
//做点什么
最终字符串searchText=newText;
if(newText.equals(“”){
suggestapter.removeAll();
suggestapter.notifyDataSetChanged();
返回true;
}
if(newText.length()<4){
返回true;
}
如果(!last_onTextChanged.equals(newText))
removeCallbacksAndMessages(null);
其他的
返回true;
last_onTextChanged=新文本;
postDelayed(新的Runnable(){
@凌驾
公开募捐{
getSearchSuggestions(searchText);
}
},800);
返回true;
}
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
//做点什么
startSearch(查询,true,null,false);
返回true;
}
};
setOnQueryTextListener(queryTextListener);
以下内容在自定义操作栏布局自定义搜索\u操作\u Bar.xml中定义
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>
以下内容在res/xml/searchable.xml中定义
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name">
</searchable>
<activity
android:name=".view.activity.tabsearch.SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:parentActivityName=".view.activity.tabsearch.SearchActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rayat.pricewiz.view.activity.tabsearch.SearchActivity" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<provider android:name="com.rayat.pricewiz.provider.SearchContentProvider"
android:authorities="com.rayat.pricewiz.provider.SearchContentProvider" />
</application>
最后在AndroidManifest.xml中
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name">
</searchable>
<activity
android:name=".view.activity.tabsearch.SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:parentActivityName=".view.activity.tabsearch.SearchActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.rayat.pricewiz.view.activity.tabsearch.SearchActivity" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<provider android:name="com.rayat.pricewiz.provider.SearchContentProvider"
android:authorities="com.rayat.pricewiz.provider.SearchContentProvider" />
</application>
非常感谢您提供的任何帮助您是否已将此用于主要活动的信息放入Manifest.xml中
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="12dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="@+id/ic_abc_button"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode_icon"
android:layout_toEndOf="@+id/txtSearch"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:layout_marginStart="0dp"
android:background="@color/logo_green"
android:layout_centerVertical="true"
android:id="@+id/ic_barcode_button"/>
</LinearLayout>
</RelativeLayout>
你找到答案了吗?没有,我最终添加了自己的实现,手动添加操作栏中的编辑文本和按钮,并处理搜索查询。重写活动的handleIntent
方法,以防止搜索结果活动打开其自身的另一个活动实例是的,在这里也是一样,我也这样做了。