gpt4 book ai didi

Android 网页 View 用户界面

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:43 25 4
gpt4 key购买 nike

我在这里用头撞墙。我只是在创建一个 WebView,它也具有前进、刷新和后退的能力。我已经使用 Android 开发工具包玩了两个星期了,感觉我还差得远,但无论我做什么,我似乎都无法解决这个问题。

我只想按菜单按钮并获得“后退”、“刷新”和“前进”选项。到目前为止,我已经设法让按钮出现在我的 WebView 中,但是它们没有执行任何操作,因为没有任何反应。对于新手 [到处搜索] 的任何解决方案都会很棒。

我的代码如下:XXXXX.java

package com.AFMoB.XXXXX;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class XXXXX extends Activity { //** Called when the activity is first created. */
public WebView webView; //DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Part of the progress bar system for future use--> this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true); // Enables Java
webView.setWebViewClient(new WebViewClient()); // Opens web links clicked by user in the webview
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) { // Handle the error
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.afdc.energy.gov/afdc/locator/m/stations/");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
menu.add(0, 1, 0, "Back");
menu.add(0, 2, 0, "Refresh");
menu.add(0, 3, 0, "Forward");
return true; // End of menu configuration
}

public boolean onCreateOptionsMenu(MenuItem item) { // Called when you tap a menu item
switch (item.getItemId()) {
case 1: //If the ID equals 1 , go back
webView.canGoBack();
item.setIcon(R.drawable.back); // Occurs after tapping the back menu item
return true;
case 2: //If the ID equals 2 , go refresh
webView.reload();
item.setIcon(R.drawable.refresh);
return true;
case 3: //If the ID equals 3 , go forward
webView.canGoForward();
item.setIcon(R.drawable.forward);
return true;
}
return false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
webView.goBack();
return true;
} // If it wasn't the BACK key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"></WebView>

<ProgressBar
android:id="@+id/myProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AFMoB.XXXXX"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">

<activity
android:name=".XXXXX"
android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

最佳答案

你评论的地方
"//当您点击菜单项时调用"
您正在调用 onCreateOptionsMenu,
应该是

public boolean onOptionsItemSelected(MenuItem item) {}

关于Android 网页 View 用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com