简体   繁体   中英

How to Set Linear Layout with WebView for onClickListener

I have a linear layout carrying webview. I have set the id of the LinearLayout to 'lay001'. My intention is to set the onClickListener of the layout so that onclick, the intent will send data to webview and then to the url i specified.

All my efforts to make it work failed me. Please help me set the java code. Thanks

activity_main.xml

<LinearLayout
        android:id="@+id/lay001"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:orientation="vertical"
        android:background="@drawable/layout_bg1">

                <WebView
                    android:id="@+id/webview001"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:accessibilityPaneTitle="The Liturgy"
                    android:layout_marginTop="2dp">
                </WebView>

                <TextView
                    android:id="@+id/textView001"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="top"
                    android:text="Anglican Hymns Tunes"
                    android:textSize="10sp"
                    android:textStyle="italic|bold"
                    android:textAlignment="center"
                    android:textColor="#DD1A1A"/>
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private WebView webview;
    private LinearLayout myLayout;

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

        myLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                     webview = (WebView001) findViewById(R.id.webview001);
                     webview.setWebViewClient(new WebViewClient());
                     webview.loadUrl("http://www.google.com");
                }
            }
        });
    }
} 

You are missing findViewById() of linearlayout.

Try this out :

   private LinearLayout myLayout;
   myLayout= (LinearLayout) findViewById(R.id.lay001);

Also change this :

    webview = (WebView) findViewById(R.id.webview001);

To load url :

 myLayout.setOnClickListener(new OnClickListener() 
     {

        @Override
        public void onClick(View v)
        {
          mWebview.loadUrl("your url");
        }
    }); 

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