gpt4 book ai didi

Android SearchView 小部件不起作用

转载 作者:行者123 更新时间:2023-11-29 20:31:45 25 4
gpt4 key购买 nike

我在操作栏中获取搜索 View 时遇到问题...

问题是,我的应用显示了搜索 View ,但是当我尝试搜索时没有任何反应。

这是 SearchActivity 类:

public class MainActivity extends AppCompatActivity
implements SearchListFragment.OnCardSelectedListener
{
private static final String TAG = "MainActivity";
private static final String idLanguage = "1";

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

// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.body_fragment) != null)
{
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null)
{
return;
}
}
}

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

// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));

searchView.setIconifiedByDefault(false);

return super.onCreateOptionsMenu(menu);
}
}

这是我的 list 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.prindl.magiccardmarket"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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

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

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

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

</activity>

<activity
android:name=".SearchResultsActivity"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity" >

<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>

最后是我的 SearchActivity 类:

package eu.mcm.prindl.magiccardmarket;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

/**
* Created by konstantin on 8/5/15.
*/

public class SearchResultsActivity extends Activity
{
private static final String TAG = "SearchResultsActivity";

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

handleIntent(getIntent());
}

@Override
protected void onNewIntent (Intent intent)
{
//super.onNewIntent(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);
Debug.write(TAG,"Search Query: " + query);

}

}
}

请帮忙。我在 Stackoverflow 上寻找过各种解决方案,但似乎都没有用(但也许我只是错过了一些东西)。

更新:这是我的 searchable.xml:

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

最佳答案

您的 list 应该看起来更像下面包含的内容:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<application android:name="..." android:icon="@drawable/logo" android:label="@string/app_name" android:theme="@style/...">
<activity android:name=".MainActivity" android:label="@string/app_name"
android:launchMode="singleTop" android:theme="@style/AppTheme">
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
...
</application>

匹配的 XML 文件包含类似的内容

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

还要确保您的 Activity 确实设置了搜索 View :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
//TODO: Reset your views
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false; //do the default
}

@Override
public boolean onQueryTextChange(String s) {
//NOTE: doing anything here is optional, onNewIntent is the important bit
if (s.length() > 1) { //2 chars or more
//TODO: filter/return results
} else if (s.length() == 0) {
//TODO: reset the displayed data
}
return false;
}

});
}
}
return true;
}

关于Android SearchView 小部件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31838717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com