我已经在我的应用程序中实现了一个搜索系统,并且出现了搜索栏,但是当按下 Enter 时,搜索活动不会加载。
除此之外,在搜索框中输入的文本也是黑色的。操作栏也是黑色的,那么有没有办法在 API 14+ 上让它变成白色?
我的主要活动:
package com.liamwli.spotify.spotifycommunity;
import java.net.MalformedURLException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
ProgressBar progress;
WebView wv;
WebSettings wvs;
String url, prefix;
SharedPreferences prefs;
SharedPreferences.Editor editor;
ActionBar ab;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
editor = prefs.edit();
ab = getActionBar();
ab.setHomeButtonEnabled(true);
progress = (ProgressBar) findViewById(R.id.pBProgress);
wv = (WebView) findViewById(R.id.wVPage);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (!url.contains("community.spotify.com")
&& !url.contains("facebook.com")
&& !url.contains("spotify.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
i.addCategory(Intent.CATEGORY_BROWSABLE);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
} else {
view.loadUrl(url);
}
return super.shouldOverrideUrlLoading(view, url);
}
});
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
if (newProgress < 100
&& progress.getVisibility() == ProgressBar.GONE) {
progress.setVisibility(ProgressBar.VISIBLE);
}
progress.setProgress(newProgress);
if (newProgress == 100) {
progress.setVisibility(ProgressBar.GONE);
}
super.onProgressChanged(view, newProgress);
}
});
wvs = wv.getSettings();
if (prefs.getBoolean("javascript_enabled", true)) {
wvs.setJavaScriptEnabled(true);
wvs.setJavaScriptCanOpenWindowsAutomatically(true);
}
if (Build.VERSION.SDK_INT >= 11)
wvs.setDisplayZoomControls(true);
wvs.setSupportZoom(true);
wvs.setSupportMultipleWindows(true);
if (prefs.getBoolean("desktop_mode", false)) {
prefix = "/?device-view=desktop";
} else {
prefix = "/?device-view=mobile";
}
url = "http://community.spotify.com" + prefix;
Intent liam = getIntent();
if (liam.getAction() == Intent.ACTION_VIEW) {
Uri data = liam.getData();
URL urlurl = null;
try {
urlurl = new URL(data.getScheme(), data.getHost(),
data.getPath());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("Malformed URL Given");
}
url = urlurl.toString() + prefix;
}
if (url == null)
throw new RuntimeException("URL Null");
wv.loadUrl(url);
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.activity_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the
// widget;
// expand it by default
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_back:
if (wv.canGoBack()) {
wv.goBack();
} else {
Toast.makeText(this, "Can't Go Back!", Toast.LENGTH_SHORT)
.show();
}
break;
case R.id.menu_forward:
if (wv.canGoForward()) {
wv.goForward();
} else {
Toast.makeText(this, "Can't Go Forward!", Toast.LENGTH_SHORT)
.show();
}
break;
case R.id.menu_refresh:
wv.reload();
break;
case R.id.menu_settings:
Intent i = new Intent(this, PrefActivity.class);
startActivity(i);
break;
case R.id.menu_search:
onSearchRequested();
break;
case android.R.id.home:
wv.loadUrl("http://community.spotify.com" + prefix);
break;
}
return super.onOptionsItemSelected(item);
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.liamwli.spotify.spotifycommunity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.liamwli.spotify.spotifycommunity.StartActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.liamwli.spotify.spotifycommunity.MainActivity"
android:excludeFromRecents="true"
android:exported="true"
android:label="@string/app_name"
tools:ignore="ExportedActivity" >
<intent-filter>
<action android:name="com.liamwli.spotify.spotifycommunity.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
<activity
android:name="com.liamwli.spotify.spotifycommunity.PostUrl"
android:excludeFromRecents="true"
android:exported="false"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.liamwli.spotify.spotifycommunity.POSTURL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.liamwli.spotify.spotifycommunity.PrefActivity"
android:exported="false"
android:label="@string/app_name" >
<intent-filer>
<action android:name="com.liamwli.spotify.spotifycommunity.PREFACTIVITY" />
<category android:name="android.intent.cetagory.PREFERENCE" />
</intent-filer>
</activity>
<activity
android:name="com.liamwli.spotify.spotifycommunity.ClearData"
android:excludeFromRecents="true"
android:exported="false"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.liamwli.spotify.spotifycommunity.CLEARDATA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.liamwli.spotify.spotifycommunity.SearchActivity"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<provider
android:name="com.liamwli.spotify.spotifycommunity.MySuggestionProvider"
android:authorities="com.liamwli.spotify.spotifycommunity.MySuggestionProvider"
android:exported="true" />
</application>
</manifest>
我的搜索活动:
package com.liamwli.spotify.spotifycommunity;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.SearchRecentSuggestions;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;
public class SearchActivity extends Activity {
WebView wv;
SharedPreferences prefs;
SharedPreferences.Editor editor;
String prefix;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
editor = prefs.edit();
wv = (WebView) findViewById(R.id.wVPage);
if (prefs.getBoolean("desktop_site", false)) {
prefix = "/?device-view=desktop";
} else {
prefix = "/?device-view=mobile";
}
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
this, MySuggestionProvider.AUTHORITY,
MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
doMySearch(query);
}
}
private void doMySearch(String query) {
// TODO Auto-generated method stub
wv.loadUrl("http://community.spotify.com/t5/forums/searchpage/tab/message?q="
+ query + prefix);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.activity_main_nosearch, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_back:
if (wv.canGoBack()) {
wv.goBack();
} else {
Toast.makeText(this, "Can't Go Back!", Toast.LENGTH_SHORT)
.show();
}
break;
case R.id.menu_forward:
if (wv.canGoForward()) {
wv.goForward();
} else {
Toast.makeText(this, "Can't Go Forward!", Toast.LENGTH_SHORT)
.show();
}
break;
case R.id.menu_refresh:
wv.reload();
break;
case R.id.menu_settings:
Intent i = new Intent(this, PrefActivity.class);
startActivity(i);
break;
case android.R.id.home:
wv.loadUrl("http://community.spotify.com" + prefix);
break;
}
return super.onOptionsItemSelected(item);
}
}
我没有收到强制关闭 - 它只是没有启动搜索活动。
另外,我应该为最终提供者设置什么权限?android系统必须能够执行,