如何将html字符串传递给另一个活动webView

时间:2015-06-28 15:36:22

标签: java android html webview

我想将html字符串传递给另一个活动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>
&#13;
&#13;
&#13;

Main_Activity.Java

&#13;
&#13;
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);
			}
		});
		
	
	}

}
&#13;
&#13;
&#13;

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

2 个答案:

答案 0 :(得分:0)

在布局文件夹中创建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");

    }

}

答案 1 :(得分:0)

你可以在第二个Activity中使用String myString = getIntent().getStringExtra("header");,但如果你的html代码在string.xml中,你可以像在第一个活动中那样得到它 - String myString = getString(R.string.html_data);它会给你同样的。

相关问题