这是我的代码:
public void gotoalbanian (View view) {
if (toggleButton.isChecked()) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("selectedlanguage", "Albanian");
editor.commit();
Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " is the default page", Toast.LENGTH_SHORT).show();
setContentView(R.layout.browser);
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
}
else {
setContentView(R.layout.browser);
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
}
}
这是返回按钮:
public void gotomain (View view) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
if (sharedPref.getString("selectedlanguage", null) != null) {
Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " no longer default", Toast.LENGTH_SHORT).show();
}
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setWebViewClient(new WebViewClient());
myWebView.destroy();
editor.clear();
editor.commit();
setContentView(R.layout.content_main);
}
用户可以切换按钮,以便应用程序保存选择,当我启动它并切换按钮以便保存,工作正常但是如果我使用gotomain
返回并启用或禁用切换按钮结果不会改变,应用程序将执行,但按钮是在使用gotomain
之前。如果已启用,即使您禁用它也会启用,反之亦然。
谢谢。
答案 0 :(得分:2)
它无效,因为您要为setContentView
,(setContentView(R.layout.browser);
)提供新的布局。这将为您的Activity
创建一个新视图,使保留引用无效。您可以做的是将WebView
添加到您的" main"布局,您在onCreate
中设置的布局,并根据切换按钮的状态更改其组件的可见性。如果您有更复杂的用例,则应使用Fragment
s
答案 1 :(得分:0)
setContentView
每个活动只应调用一次。在onCreate函数中创建contentView。将对WebView的引用保存为属性(并保存已检查的状态)。
也许尝试这样的事情:
private boolean isChecked = false;
private WebView myWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
myWebView = (WebView) findViewById(R.id.webView2);
toggleButton = ??;
}
public void onResume() {
toggleButton.setChecked(isChecked);
}
public void gotoalbanian (View view) {
isChecked = toggleButton.isChecked();
if (toggleButton.isChecked()) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("selectedlanguage", "Albanian");
editor.commit();
Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " is the default page", Toast.LENGTH_SHORT).show();
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
} else {
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.example.com");
}
}
public void gotomain (View view) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
if (sharedPref.getString("selectedlanguage", null) != null) {
Toast.makeText(getBaseContext(), sharedPref.getString("selectedlanguage", null) + " no longer default", Toast.LENGTH_SHORT).show();
}
myWebView.setWebViewClient(new WebViewClient());
myWebView.destroy();
editor.clear();
editor.commit();
}