我为android创建了一个 [very] 简单应用程序,它连接到首选项中保存的URL。我已经能够让应用程序打开一个新的活动/布局来输入URL并保存它,然后我使用finish();返回主屏幕(WebView)。
如何在设置新网址后刷新WebView?
[编辑]
我认为我的偏好不会被保存:
mySettings.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>
有什么想法吗?
答案 0 :(得分:0)
您只能在您的Activity类中获取您的Web视图的引用,或者参考您的活动,这样做:
WebView webView = (WebView)findViewById(R.id.my_web_view);
然后,重新加载您的网络视图:
webView.loadUrl("http://www.websitehere.php");
或更简单:
webView.reload();
答案 1 :(得分:0)
您遇到的问题是您正在引用两个不同的SharedPreferences。您将它存储在设置活动中的默认共享首选项(这是好的)中,然后从您自己的自定义首选项中检索它。只需两次调用相同的默认共享首选项。以下是您需要做的事情:
更改
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
你的mainActivity中的:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
并更改:
Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
在mySettings活动中:
Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
一切都应该开始正常工作了。