android搜索活动工具栏不响应点击

mxg2im7a  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(162)

我已经创建了一个搜索活动,但是按下工具栏中的任何图标都不会注册任何内容。
我在onoptionsitemselected函数的第一行放了一个log.d,但是没有打印出任何东西,这表明该函数没有被调用。我知道调用oncreate和searchrules是因为searchrules中的log.d打印出查询。我在任何其他活动(包括main、settings和几个自定义活动)中都没有遇到不注册单击的问题。
我遵循了官方开发人员指南中的建议,创建这个活动并将其集成到应用程序的其余部分。
主要活动的相关部分。

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem search = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) search.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return true;
}

android清单的相关部分

<activity android:name=".SearchableActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>
<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=".SearchableActivity" />
</activity>

可搜索的活动布局,命名为activity\u searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".SearchableActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

可搜索活动,名为searchableactivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;

import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class SearchableActivity extends AppCompatActivity {

int mThemeID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mThemeID = setTheme();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchable);

    // setup toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

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

private int setTheme() {
    SharedPreferences sharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(this);
    String themeColor = sharedPreferences.getString("color", "");
    ParseTheme parseTheme = new ParseTheme();
    int themeID = parseTheme.parseThemeColor(themeColor);
    super.setTheme(themeID);

    return themeID;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO the toolbar isn't registering presses
    Intent intent;
    switch(item.getItemId()) {
        case android.R.id.home:
            finish();
            return super.onOptionsItemSelected(item);
        case R.id.action_settings:
            intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void searchRules(String query) {
    // TODO search xml and json
    Log.d("Search", query);
}

}

谢谢你能给我的任何帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题