将多个textview链接到单个webview

时间:2016-04-11 04:54:57

标签: android android-webview

    public class MainActivity extends Activity {
        TextView textview,textView5;/* My textviews*/

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textview=(TextView)findViewById(R.id.textView);
            textview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,Amazon.class);
                    startActivity(intent);/* to connect webview in the next intent*/
                    Intent intent3 = new Intent(MainActivity.this, Amazon.class);
                    intent3.putExtra("webviewurl", webviewurl);
                    startActivity(intent3);

                }
            });
            textView5=(TextView)findViewById(R.id.textView5);
           textView5.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent1=new Intent(MainActivity.this,Amazon.class);
                   startActivity(intent1);/* to start the intent*/
               }
           });

        }

}

我需要知道多个文本视图如何链接到具有不同网址的下一个活动中的单个Web视图。

1 个答案:

答案 0 :(得分:2)

以下示例:

main_activity.layout

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 1"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 2"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 3"/>

</LinearLayout>
MainActivity.java中的

public class MainActivity extends Activity {
    TextView textview1,textview2,textview3,textView5;/* My textviews*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview1=(TextView)findViewById(R.id.textView1);
        textview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {               
                Intent intent1 = new Intent(MainActivity.this, Amazon.class);
                intent1.putExtra("webviewurl", "your URL for Textview 1");
                startActivity(intent1);

            }
        });

       textView2=(TextView)findViewById(R.id.textView2);
       textView2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent1 = new Intent(MainActivity.this, Amazon.class);
                intent1.putExtra("webviewurl", "your URL for Textview 2");
                startActivity(intent1);
           }
       });

        textView3=(TextView)findViewById(R.id.textView3);
       textView3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent1 = new Intent(MainActivity.this, Amazon.class);
                intent1.putExtra("webviewurl", "your URL for Textview 3");
                startActivity(intent1);
           }
       });
    }

在亚马逊上,在onCreate

添加此代码
String mURL =  getIntent().getStringExtra("webviewurl");
webView = (WebView) findViewById(R.id.webView); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(mURL);