使用WebView onTouchListener链接到另一个Activity - Android

时间:2013-12-19 09:49:08

标签: java android android-intent webview ontouchlistener

我有这个主页面,我设置了多个webViews。基本上这个想法很简单。我使用三个webViews,所有这些都从在线网站获取不同的内容。他们点击Web视图的那一刻,应该将他们带到另一个活动。然后把剩下的留给我。我只需要帮助您使用onTouchListener将Web视图链接到另一个活动。

这是我的Java文件:查找// THE CODE FOR THE OnTOUCHEVETN STARTS HERE!行。这是我尝试我的webview 1开始第二个活动的地方。我试图使用它:Intent myIntentActivity1 = new Intent(this, ReadComments.Class);但是java给了我以下错误:

  

删除与'Intent()'匹配的参数

package com.testapp;

import com.testapp.R.menu;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

public class mainlogin extends Activity implements OnClickListener{

private Button  mRegister, mlogin, mcontactus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlogin);


    ImageView img = (ImageView)findViewById(R.id.centennialmsssite);
    img.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("http://mywebsite.com"));
            startActivity(intent);
        }

    });

    WebView wv = (WebView)findViewById(R.id.my_webview);
    wv.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv.loadUrl("http://mywebsite.com/main-content");

    // THE CODE FOR THE OnTOUCHEVETN STARTS HERE!

    WebView wv1 = (WebView)findViewById(R.id.topic_one);
    wv1.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv1.loadUrl("http://mywebsite.com/content-one");

    wv1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){

                Intent myIntentActivity1 = new Intent(this, ReadComments.Class);
                //call N_X and wait for result
                startActivity(myIntentActivity1);

                return true;
            }
            return false;
        }
    });

    // IT ENDS HERE!

    WebView wv2 = (WebView)findViewById(R.id.topic_two);
    wv2.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv2.loadUrl("http://mywebsite.com/content-two");

    WebView wv3 = (WebView)findViewById(R.id.topic_three);
    wv3.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
            }
    });

    wv3.loadUrl("http://mywebsite.com/content-three");

    mRegister = (Button)findViewById(R.id.btn_register);
    mRegister.setOnClickListener(this);

    mlogin = (Button)findViewById(R.id.btn_login);
    mlogin.setOnClickListener(this);

    mcontactus = (Button) findViewById(R.id.btn_contact_us);
    mcontactus.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_login:
        Intent i = new Intent(this, Login.class);
        startActivity(i);
        break;
    case R.id.btn_register:
        Intent i1 = new Intent(this, Register.class);
        startActivity(i1);
        break;
    case R.id.btn_contact_us:
        Intent i2 = new Intent(this, contactus.class);
        startActivity(i2);
        break;
    default:
        break;
    }

}
}

这是我的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<Button
    android:id="@+id/btn_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btn_login"
    android:layout_alignBottom="@+id/btn_login"
    android:layout_alignRight="@+id/textView1"
    android:background="#333333"
    android:minHeight="40dp"
    android:minWidth="140dp"
    android:text="@string/mainpage_register_button"
    android:textColor="#999999" />

<Button
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="280dp"
    android:background="#333333"
    android:minHeight="40dp"
    android:minWidth="140dp"
    android:text="@string/main_button_login"
    android:textColor="#999999" />

<Button
    android:id="@+id/btn_contact_us"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn_login"
    android:layout_alignRight="@+id/btn_register"
    android:layout_below="@+id/btn_register"
    android:background="#333333"
    android:minHeight="40dp"
    android:layout_marginTop="2dp"
    android:text="@string/mainpage_contact_us"
    android:textColor="#999999" />

<ImageView
    android:id="@+id/centennialmsssite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn_contact_us"
    android:layout_below="@+id/btn_contact_us"
    android:layout_marginTop="14dp"
    android:contentDescription="@string/mss_button_description"
    android:src="@drawable/msswebsitebutton" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="286dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:gravity="center"
    android:text="@string/welcome_mainpage"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff"
    android:textStyle="bold" />

<WebView
    android:id="@+id/my_webview"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/sitebutton"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="12dp"
    android:maxHeight="40dp"
    android:minHeight="40dp" />

<WebView
    android:id="@+id/topic_one"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/my_webview"
    android:layout_alignRight="@+id/my_webview"
    android:layout_below="@+id/my_webview"
    android:layout_marginTop="5dp"
    android:maxHeight="70dp"
    android:minHeight="70dp" />

<WebView
    android:id="@+id/topic_two"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/topic_one"
    android:layout_alignRight="@+id/topic_one"
    android:layout_below="@+id/topic_one"
    android:maxHeight="70dp"
    android:minHeight="70dp" />

    <WebView
        android:id="@+id/topic_three"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/topic_two"
        android:layout_alignRight="@+id/topic_two"
        android:layout_below="@+id/topic_two"
        android:maxHeight="70dp"
        android:minHeight="70dp" />

</RelativeLayout>

可能导致此错误的原因是什么?

1 个答案:

答案 0 :(得分:2)

试试这个 -

Intent myIntentActivity1 = new Intent(mainlogin.this, ReadComments.class);
startActivity(myIntentActivity1);

或者 -

Intent myIntentActivity1 = new Intent(getBaseContext(), ReadComments.class);
startActivity(myIntentActivity1);