简体   繁体   中英

how to pass html string to another activity webView

I want to pass html string to another activity webView. Here my html string and Main_Activity Code. What is Another_Activity.java Code.

 <?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); } }); } } 

Please Write Another_Activity.Java code to show WebView text.

Create webview.xml in layout folder

<?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"
/>

then create Second_Activity like this.

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");

    }

}

You can use String myString = getIntent().getStringExtra("header"); in the second Activity, but if your html code is in string.xml, you just can get it as you do in the first activity - String myString = getString(R.string.html_data); and it will gives you the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM