简体   繁体   English

在android searchview中没有触发Intent

[英]Intent is not triggered in android searchview

Here is my code for searchview in android.Search option is visible on the toolbar but when a text and entered and searched it does not trigger SearchActivity . 这是我在android.Search选项中的searchview代码在toolbar上可见但是当输入和搜索文本时它不会触发SearchActivity

xml/searchable.xml XML / searchable.xml

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

menu/menu.xml 菜单/ 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/search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

AndroidManifest.xml AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.welcomecure.searchview">

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

        <activity android:name=".MainActivity">
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />

          </intent-filter>
            <meta-data
              android:name="android.app.default_searchable"
              android:value=".SearchResultsActivity" />
        </activity>

        <activity
          android:name=".SearchActivity"
          android:label="@string/app_name">

          <!-- to identify this activity as "searchable" -->
          <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>

</manifest>

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.menu, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    return true;
 }
}

SearchActivity.java SearchActivity.java

public class SearchActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handleIntent(getIntent());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    return true;
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search
    }
 }
}

try to use the same activity name from the calling activity, you used SearchActivity to show the search result, try to use the same activity name 尝试使用来自调用活动的相同活动名称,您使用SearchActivity显示搜索结果,尝试使用相同的活动名称

<activity android:name=".MainActivity">
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />

          </intent-filter>
            <meta-data
              android:name="android.app.default_searchable"
              android:value=".SearchActivity" />
</activity>

Your Activity is named SearchActivity , but you declared SearchResultsActivity in manifest: 您的活动名为SearchActivity ,但您在清单中声明了SearchResultsActivity

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

From the docs : 来自文档

To declare the searchable activity for an activity's search dialog, add a element inside the respective activity's element. 要为活动的搜索对话框声明可搜索活动,请在相应活动的元素内添加元素。 The element must include the android:value attribute that specifies the searchable activity's class name and the android:name attribute with a value of "android.app.default_searchable". 该元素必须包含指定可搜索活动的类名的android:value属性和值为“android.app.default_searchable”的android:name属性。

You can also do this programmatically. 您也可以通过编程方式执行此操作。

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) { // this is triggered when you press the key search from keyboard.
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) { // this is triggered when you change your search text. 
            return true;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM