0
我仍在學習Java。我試圖讓SharedPreferences在我的WebView中設置url,以便我可以設置下次啓動應用程序時顯示的頁面。 並嘗試使用菜單設置/更改值。將Webview開始網址添加到webview的sharedpreference
這是我到目前爲止有:
public class test extends Activity {
public static final String PREFS_NAME = "SharedPreferences";
public static final String PREF_STRING = "http://www.google.com";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPrefs = getSharedPreferences(PREFS_NAME, 0);
webview = new WebView(this) {
};
webChromeClient = new WebChromeClient() {
};
webview.setWebChromeClient(webChromeClient);
webview.setWebViewClient(webViewClient);
setContentView(webview);
/** Load the Browser Settings */
webview.loadUrl(PREF_STRING);
SharedPreferences settings = getSharedPreferences("StartPage", 0);
settings.getString("PREF_STRING", "http://www.google.com");
}
/** Begin Globals */
public WebView webview;
public WebChromeClient webChromeClient;
public WebViewClient webViewClient;
/*** define menu options */
private static final int MENU_PAGE1 = 1;
private static final int MENU_PAGE2 = 2;
/*** menu items */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_PAGE1, 0, "Set page 1 to show when app is started");
menu.add(0, MENU_PAGE2, 0, "Set page 2 to show when app is started");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_PAGE1:
Editor e = mPrefs.edit();
e.putString(PREF_STRING, "http://www.yahoo.com");
e.commit();
Toast.makeText(this, "Page 1 is set!", Toast.LENGTH_SHORT).show();
return true;
}
switch (item.getItemId()) {
case MENU_PAGE2:
Editor e = mPrefs.edit();
e.putString(PREF_STRING, "http://www.ask.com");
e.commit();
Toast.makeText(this, "Page 2 is set!", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
如此接近的是,到目前爲止...任何提示或想法?
你想從sharepref獲取網址並在webview中打開? – 2011-06-04 05:54:03
@CapDroid種類,我想它將我的選擇存儲在菜單中,然後顯示我重新啓動手機和應用程序後選擇的頁面。 – Colby 2011-06-04 06:09:43