我会添加什么代码,以便在按下菜单按钮刷新时刷新我应用程序中的webview。?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WV = (WebView) findViewById(R.id.webView1);
WV.getSettings().setJavaScriptEnabled(true);
WV.loadUrl("http://home.btconnect.com/MrJFisher/");
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
WebView.reload();
}
return super.onOptionsItemSelected(item);
}
};
初学者。所以我道歉。
答案 0 :(得分:2)
修改强>:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://home.btconnect.com/MrJFisher/");
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
mWebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 1 :(得分:1)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== R.id.item1){
WV.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
请参阅WebView docs