我有一个带有一些自定义选项的Webview,如下所示:
public void WebViewLoad (View view) {
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setFocusable(true);
myWebView.setFocusableInTouchMode(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setVisibility(View.VISIBLE);
backtoLanguage.setVisibility(View.VISIBLE);
myWebView.loadUrl(link); //<-- Load defined URL as link
}
它的隐藏式直到WebViewLoad被调用,但我想用link
从
public void gotoEnglish() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("selectedlanguage", LanguageFactory.LANGUAGE_ENGLISH);
editor.commit();
//load WebWiewLoad with URL defined here
}
代码中可能存在一些错误,我对此很陌生......
非常感谢你。
更新
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private Button backtoLanguage;
private static Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
backtoLanguage = (Button) findViewById(R.id.backtoLanguage);
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setVisibility(View.GONE);
backtoLanguage.setVisibility(View.GONE);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int languageCode = sharedPref.getInt("selectedlanguage", -1);
//LanguageFactory factory = new LanguageFactory();
//Language language = factory.getLanguage(languageCode);
if (languageCode == (100)){
Toast.makeText(getApplicationContext(), "English is set to default launch page" ,Toast.LENGTH_LONG ).show();
English();
}
if (languageCode == (101)){
Toast.makeText(getApplicationContext(), "French is set to default launch page" ,Toast.LENGTH_LONG ).show();
French();
}
}
WebView myWebView;
public void WebViewLoad (View view) {
WebView myWebView = (WebView) findViewById(R.id.webView2);
myWebView.setFocusable(true);
myWebView.setFocusableInTouchMode(true);
myWebView.getSettings().setJavaScriptEnabled(true);
//myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setVisibility(View.VISIBLE);
backtoLanguage.setVisibility(View.VISIBLE);
}
public interface Language {
void speak();
}
public class English implements Language {
@Override
public void speak() {
Log.i("Tag", "English");
}
}
public class French implements Language {
@Override
public void speak() {
Log.i("Tag", "French");
}
}
public void gotoenglish(View view) {
if (toggleButton.isChecked()) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("selectedlanguage", LanguageFactory.LANGUAGE_ENGLISH);
editor.commit();
English();
} else English();
}
public void English() {
myWebView.loadUrl("http://www.example.com/");
}
我已经按照你们的要求包含了大部分代码。
答案 0 :(得分:2)
将对象myWebView
声明为全局变量。然后您可以在gotoEnglish()
方法中轻松访问它以加载URL
以下是示例代码
WebView myWebView
public void WebViewLoad (View view) {
myWebView = (WebView) findViewById(R.id.webView2);
...
}
public void gotoEnglish() {
...
myWebView.loadUrl(link); //<-- Load defined URL as link
}
希望这个帮助
答案 1 :(得分:0)
我已经使用这种方式存档:
private void go(String strUrl) {
myWebView.loadUrl(strUrl);
}
public void English() {
go("http://www.example.com/");
}