Android Webview:点击webview后,应打开新活动

时间:2015-11-05 10:24:34

标签: android webview android-webview

我是Android开发的新手,我遇到了android webview的问题。 我想点击它从webview回来,但它无法正常工作。 请检查我的代码告诉我,我做错了什么。

package eukti.myafterclass.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import java.util.logging.Handler;

import eukti.myafterclass.R;

public class PromotionActivity extends AppCompatActivity  implements View.OnTouchListener {

    private static final int CLICK_ON_WEBVIEW = 1;
    private static final int CLICK_ON_URL = 2;

    private WebView wv1;
    private WebViewClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promotion);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        /*Button rbutton = (Button) findViewById(R.id.redirectbutton);
        rbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(PromotionActivity.this, DashBoardMainActivity.class);
                startActivity(intent);
                finish();
            }

        });*/

        wv1=(WebView)findViewById(R.id.webview);
        wv1.setWebViewClient(new MyBrowser());

        String url = "http://myafterclass.com/privacy-policy/";

        wv1.getSettings().setLoadsImagesAutomatically(true);
        wv1.getSettings().setJavaScriptEnabled(true);
        wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        wv1.loadUrl(url);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.webview && event.getAction() == MotionEvent.ACTION_DOWN){
            Intent intent = new Intent(PromotionActivity.this, DashBoardMainActivity.class);
            startActivity(intent);
            finish();
        }
        return false;
    }

    public boolean handleMessage(Message msg) {
        if (msg.what == CLICK_ON_URL){
            Intent intent = new Intent(PromotionActivity.this, DashBoardMainActivity.class);
            startActivity(intent);
            finish();
            return true;
        }
        if (msg.what == CLICK_ON_WEBVIEW){
            //Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(PromotionActivity.this, DashBoardMainActivity.class);
            startActivity(intent);
            finish();
            return true;
        }
        return false;
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}

如果您需要任何其他详细信息,可以问我。

1 个答案:

答案 0 :(得分:1)

您忘记了这一行wv1.setOnTouchListener(this);