如何使用 WebView 为 onClickListener 设置线性布局

How to Set Linear Layout with WebView for onClickListener

我有一个带有webview的线性布局。我已将 LinearLayout 的 ID 设置为 'lay001'。 我的意图是设置布局的onClickListener,以便onclick,意图将数据发送到webview,然后发送到我指定的url。

我为让它工作所做的所有努力都失败了。请帮我设置 java 代码。谢谢

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

您缺少线性布局的 findViewById()。

试试这个:

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

也改一下:

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

加载url:

 myLayout.setOnClickListener(new OnClickListener() 
     {

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