无法将其链接到网页。“重置”和“关于”应该可以。使用网络视图。需要在 webview 中打开链接。
这是 MainActivity.java:
package com.test.apppackage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("https://www.google.com");
setContentView(mWebview );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.menu_reset:
mWebview .loadUrl("https://www.bbc.co.uk");
setContentView(mWebview );
break;
}
return super.onOptionsItemSelected(item);
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
这是活动 main.xml 文件
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_reset"
android:title="@string/menu_reset"
android:showAsAction="ifRoom"
/>
<item
android:id="@+id/menu_about"
android:title="@string/menu_about"
android:showAsAction="never"
/></menu>
这是 strings.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">App name</string>
<string name="menu_reset">Reset</string>
<string name="menu_about">About app</string>