我尝试在操作栏中使用搜索视图,但在我点击键盘底部的搜索按钮后,没有任何反应。我在下面发布我的代码。请忽略文件中的包名称。在我的代码中,我将searchview的值设置为textView,但未设置。 Log cat也没有显示任何其他内容。先感谢您。
Folder.java ------------------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.home, menu);
//----THIS ONE -----------
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
//----THIS ONE ---------------------------------------------------------------------------
// return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String filter_id = null,Tickets_title = null;
int start_limit=0,page_no=1;
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_search:
//Intent searchmenu = new Intent(Folders.this,SearchResultsActivity.class);
//startActivity(searchmenu);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
------- SearchResultactivity --------------------
package com.abc;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SearchResultsActivity extends Activity {
private TextView txtQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
/**
* Use this query to display search results like
* 1. Getting the data from SQLite and showing in listview
* 2. Making webrequest and displaying the data
* For now we just display the query only
*/
txtQuery.setText("Search Query: " + query);
}
}
}
---------------- Activity_search_results.xml ------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abc.Folders" >
<TextView
android:id="@+id/txtQuery"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
----------------------Searchable.xml -------------------------------
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name" />
-----------------------manifeast.xml ------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc"
android:versionCode="1"
android:versionName="1.5" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="24"/>
<!-- Creates a custom permission so only this app can receive its messages.-->
<permission
android:name="com.abc.pqr.C2D_MESSAGE"
android:protectionLevel="signature"/>
<!-- Initially app name and logo as the blank starts -->
<application
android:allowBackup="true"
android:icon="@drawable/blank3"
android:alpha="120"
android:label=""
android:theme="@style/MyTheme">
<!-- Initially app name and logo as the blank ends -->
<activity
android:name="com.abc.pqr"
android:label="" ><!-- @string/app_name --><!-- app name blank if present it shows 2-3 sec when start the activity , we have to change it -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- THIS ONE -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
<!-- THIS ONE ENDS -->
</activity>
<!-- Search results activity -->
<activity android:name=".SearchResultsActivity"
android:parentActivityName="com.abc.Folders" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
你好答案是,
只需在特定活动下的清单文件中添加以下行。您想在其中显示搜索视图。
答案 0 :(得分:0)
嗨Rajesh这是menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:panelFullBackground="#ffff" >
<!-- THIS ONE FOR SEARCH ITEMS -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<!-- THIS ONE FOR GLOBAL SEARCH ITEMS -->
</menu>
大家好,问题解决了......我刚刚加了 此代码在特定活动中,我为Search Functionaliy编写代码。