从未调用过Android搜索活动

问题描述:

在我发布之前,我尝试了所有可能的解决方案。最后我陷入了困境,没有运气。我只想在搜索活动中显示查询字符串。任何帮助?提前致谢。从未调用过Android搜索活动

的AndroidManifest.xml:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/my_app" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <meta-data 
     android:name="android.app.default_searchable" 
     android:value="com.mycompany.mygoodapp.SearchResultsActivity"/> 

    <activity 
     android:name=".MainActivity" 
     android:configChanges="orientation" 
     android:label="@string/my_app" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="stateHidden|adjustPan"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".SearchResultsActivity" 
     android:label="@string/my_app" 
     android:launchMode="singleTop"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 

    </activity> 
</application> 

SearchResultsActivity实现如下:

public class SearchResultsActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_results); 

    DisplayToast("Here ..."); 
    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 

    setIntent(intent); 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     DisplayToast(query); 

    } 

} 

public void DisplayToast(String s) { 

    Toast.makeText(SearchResultsActivity.this, 
      s, Toast.LENGTH_LONG).show(); 
    } 

} 

searchable.xml:

<?xml version="1.0" encoding="utf-8"?> 

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
android:label="@string/my_app" 
android:hint="@string/search_hint" /> 

MainActivity:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 

    getMenuInflater().inflate(R.menu.menu_main, menu); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     SearchManager searchManager = 
       (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = 
       (SearchView) menu.findItem(R.id.search).getActionView(); 
     ComponentName cn = new ComponentName("com.mycompany.mygoodapp", "com.mycompany.mygoodapp.SearchResultsActivity"); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); 

     searchView.setIconifiedByDefault(true); 

    } 

return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    /// 
    else if(id==R.id.search) 
    { 
     onSearchRequested(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

主菜单布局搜索菜单项:

<item 
    android:id="@+id/search" 
    android:title="@string/search_title" 
    android:icon="@android:drawable/ic_menu_search" 
    app:showAsAction="always" 
    app:actionViewClass="android.support.v7.widget.SearchView"> 

+1

“我尝试了所有可能的解决办法张贴在这里”你尝试过哪些解决方案? –

+0

嗨dask。只需查看本教程http://blog.nkdroidsolutions.com/android-searchview-in-listview-example-tutorial/并自己尝试一下。 – RameshJaga

+0

@dask检查我的答案 –

您可以通过以下两种方式

1路

菜单/ options_menu.xml

搜索了解创建活动
<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/action_search" 
    android:icon="@drawable/disha" 
    app:showAsAction="collapseActionView|always" 
    android:title="search" 
    app:actionViewClass="android.support.v7.widget.SearchView"/> 
</menu> 
为您的活动

Java代码

public boolean onCreateOptionsMenu(Menu menu) { 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.search_menu, menu); 

// 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())); 

return true; 
} 

在manifiest文件中添加此

<meta-data android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 

searchable.xml

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
android:label="@string/app_name" 
android:hint="search hint" /> 

使用自定义

2路动作条 或者你也可以试试这个

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

getSupportActionBar().setCustomView(R.layout.search_layout); 

search = (EditText) getSupportActionBar().getCustomView(). 
     findViewById(R.id.searchfield); 

search.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, 
            KeyEvent event) { 
     return true; 
    } 
}); 

getSupportActionBar().setDisplayOptions(getSupportActionBar().DISPLAY_SHOW_CUSTOM 
     | getSupportActionBar().DISPLAY_SHOW_HOME); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

比创建一个自定义actiob栏布局这样

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/searchfield" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/custom_search_edit" 
android:hint="@string/search_hint" 
android:inputType="textFilter" 
android:textColor="@color/colorBlack" 
android:textColorHint="@color/colorAccent" 
android:textSize="15sp" /> 

试试这个,问我任何查询的案例