码迷,mamicode.com
首页 > Web开发 > 详细

JS调用onBackPressed

时间:2019-10-24 21:42:13      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:rri   location   long   invisible   enc   near   nec   sof   使用   

需求:

安卓页面webview加载H5页面,H5页面能能返回到安卓页面

技术图片

 

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.outfit7.talkingfriends.gui.view.wardrobe.R;
import com.outfit7.talkingfriends.gui.view.wardrobe.utils.TelecomUrl;

public class TelecomActivity extends AppCompatActivity {

    /**
     * 自定义的,当js调用history.go时加载的链接,以监听history.go方法的调用
     */
    private final static String CONSTANTS_GO_BACK = "/CONSTANTS_GO_BACK#";

    private ImageView telecomBack;
    private ImageView phoneClear;
    private EditText phoneNumber;
    private TextView telecomNext;
    private LinearLayout inputLayout;
    private String phone;
    private boolean payState;
    private static final String RETURN_FLAG = "ShijilongBack";
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telecom);
        telecomBack = findViewById(R.id.telecomBack);
        phoneClear = findViewById(R.id.phoneClear);
        phoneNumber = findViewById(R.id.phoneNumber);
        telecomNext = findViewById(R.id.telecomNext);
        inputLayout = findViewById(R.id.inputLayout);

        //监听输入
        phoneNumber.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (temp.length() == 0) {
                    phoneClear.setVisibility(View.INVISIBLE);
                    telecomNext.setEnabled(false);
                } else if (temp.length() > 0 && temp.length() < 11) {
                    phoneClear.setVisibility(View.VISIBLE);
                    telecomNext.setEnabled(false);
                } else if (temp.length() == 11) {
                    phoneClear.setVisibility(View.VISIBLE);
                    telecomNext.setEnabled(true);
                }
            }
        });

        //清空输入
        phoneClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phoneNumber.setText("");
                phoneClear.setVisibility(View.INVISIBLE);
                telecomNext.setEnabled(false);
            }
        });

        //返回
        telecomBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //跳转
        telecomNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //隐藏软键盘
                hideSoftKeyboard();
                phone = phoneNumber.getText().toString();
                inputLayout.setVisibility(View.GONE);
                loadWebView();
            }
        });
    }

    private void hideSoftKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private void loadWebView() {
        webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                pageFinished(url);
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("wanglei", "url=" + url);
                //拦截支付回调
                if (url.contains(RETURN_FLAG)) {
                    int beginIndex = url.indexOf("code=") + 5;
                    String code = url.substring(beginIndex, beginIndex + 1);
                    Log.i("wanglei", "code=" + code);
                    if (!TextUtils.isEmpty(code)) {
                        payState = ("0".equals(code) ? true : false);
                        onBackPressed();
                    }
                } else {
                    overrideUrlLoading(url);
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        webView.loadUrl(TelecomUrl.assembleUrl(phone));
        setContentView(webView);
    }

    /**
     * 在WebViewClient对应方法中调用
     * 注:此方法需手动调用,或者使用defaultClient == true
     */
    public void pageFinished(String url) {
        //重写js页面window.history.go方法
        String script = "javascript:(function(){window.history.go = function(index){" +
                "window.location.href=‘" + CONSTANTS_GO_BACK + "‘+index;" +
                "}})()";
        webView.loadUrl(script);
    }

    /**
     * 在WebViewClient对应方法中调用
     * 注:此方法需手动调用,或者使用defaultClient == true
     */
    public void overrideUrlLoading(String url) {
        //拦截返回
        if (!TextUtils.isEmpty(url) && url.contains(CONSTANTS_GO_BACK)) {
            int step = -1;
            try {
                //解析js调用history.go时传入的参数
                step = Integer.parseInt(url.split("#")[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (webView.canGoBackOrForward(step)) {
                webView.goBackOrForward(step);
            } else {
                onBackPressed();
            }
        } else {
            onBackPressed();
        }
    }

    @Override
    public void onBackPressed() {
        //测试支付成功
        if (payState) {
            setResult(RESULT_OK);
        }
        super.onBackPressed();
    }
}

 

代码中有些业务逻辑,可以自行删除

JS调用onBackPressed

标签:rri   location   long   invisible   enc   near   nec   sof   使用   

原文地址:https://www.cnblogs.com/anni-qianqian/p/11734894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!