gpt4 book ai didi

android - 如何在 onitemclick 的 webview 中加载 HTML 文件

转载 作者:行者123 更新时间:2023-11-30 03:14:27 25 4
gpt4 key购买 nike

我有超过 100 个 html 文件,我希望每个文件在 ListView 中的行点击打开,每个 html 文件应该在 webview 中打开,我试过这段代码但是这不起作用,只有 webview 在行点击打开, html 文件未显示。

 package com.Example.apk;



import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebViewClient;

public class WebView extends Activity {

public class WebViewActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
// similarly for 4 and 5 and so on.
}

public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);

}
}
}
}

最佳答案

将位置传递给下一个 Activity

list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivtiy.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});

然后在WebViewActivity即activity_webview)

public class WebViewActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
...// similarly for 4 and 5 and so on.
}

public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);

}
}

WebViewActivity 的 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

在 list 文件中为 WebViewActivity 创建一个条目。

关于android - 如何在 onitemclick 的 webview 中加载 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375435/

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