gpt4 book ai didi

java - 如何将html字符串传递给另一个 Activity webView

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:51 25 4
gpt4 key购买 nike

我想将 html 字符串传递给另一个 Activity webView。这是我的 html 字符串和 Main_Activity 代码。 Another_Activity.java 代码是什么。

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">String Passing</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>

<string name="html_data">
<![CDATA[
<html>
<head></head>
<body style="text-align:justify;">
<b><u>Everton plotting loan swoop for Man United youngster James Wilson</u></b>
The Toffees were quick to register an early interest for the 19-year-old striker at the turn of the year.
Goodison Park boss Roberto Martinez has had his eye on a number of Old Trafford fringe players, although Wilson remains the most likely.
Wilson, who scored twice for United last season, is also a target for West Bromwich Albion, having been recommended to them by former United midfielder Darren Fletcher.
United will overhaul their striker department this summer following the release of Colombian striker Radamel Falcao and the continued uncertainty surrounding the future of Robin van Persie.
United have made no decision yet on Wilson and boss Louis van Gaal will assess his pre-season before sitting down with the teenager and discussing all the options.
</body>
</html>
]]>
</string>


<string name="title_activity_another_">Another_Activity</string>

</resources>

Main_Activity.Java

package com.nasir.stringpassing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button Nasir;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Nasir = (Button) findViewById(R.id.Nasir);
Nasir.setOnClickListener(new OnClickListener() {

// String myString = getString(R.string.html_data);

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent intent = new Intent(MainActivity.this, Second_Activity.class);
intent.putExtra("header", getString(R.string.html_data));
startActivity(intent);
}
});


}

}

请编写 Another_Activity.Java 代码来显示 WebView 文本。

最佳答案

在layout文件夹中创建webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

然后像这样创建 Second_Activity 。

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Second_Activity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = getIntent()..getStringExtra("header");;
webView.loadData(customHtml, "text/html", "UTF-8");

}

}

关于java - 如何将html字符串传递给另一个 Activity webView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101561/

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