我为 android 制作了一个[非常]简单的应用程序,它连接到保存在首选项中的 URL。我已经能够让应用程序打开一个新的活动/布局以输入 URL 并保存它,然后我使用 finish(); 返回主屏幕 (WebView)。
设置新 URL 后如何让 WebView 刷新?
[编辑]
我认为我的偏好没有被保存:
我的设置.java
package com.rarecreativegroup.dcm1config;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
public class mySettings extends Activity {
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu_layout);
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
String string = getString(R.string.ipaddress);
editor.putString("ipaddress", string);
editor.commit();
WebView webViewer = (WebView)findViewById(R.id.webview);
//webViewer.loadUrl(string);
finish();
}
});
}
}
mainActivity.java
package com.rarecreativegroup.dcm1config;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
public static final String PREFS_NAME = "preferences";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String uri = settings.getString("ipaddress", "");
WebView webview = new WebView(this);
webview.loadUrl(uri);
setContentView(webview);
//setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, mySettings.class));
return true;
case R.id.refresh:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String uri = settings.getString("ipaddress", "");
WebView webview = new WebView(this);
webview.loadUrl(uri);
setContentView(webview);
default:
return super.onOptionsItemSelected(item);
}
}
}
res/xml/preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="IP Settings"
android:key="ip_settings">
<EditTextPreference
android:key="ipaddress"
android:title="DCM1 IP"
android:summary="Define the IP address of the DCM1"
android:dialogTitle="DCM1 IP Address"
android:dialogMessage="Supply IP Address"
android:defaultValue="http://192.168.6.117/" />
</PreferenceCategory>
</PreferenceScreen>
有任何想法吗?