Android里面WebView加载HTML里面点击按钮调我Android方法

public class WebActivity extends Activity {

private static final String TAG = "--WebActivity--";
private WebView mWebView;
private ProgressBar progressBar;
private Activity mContext;
private TextView webTitle;

private String url;
private String keyName;
private String tvTitle;
private String actionID;
private String mOrderNumber;
private String mOrderJiaoYan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    mContext = this;

    initGetIntent();

    initView();

    initSetting();

    showWeb(actionID);
}

// 根据网页的地址,打开网页
private void showWeb(String actionID) {
    mWebView.loadUrl(actionID);
    webTitle.setText(keyName);
}

private void initGetIntent() {
    Intent intent = getIntent();
    keyName = intent.getStringExtra("keyName");
    actionID = intent.getStringExtra("actionID");
    tvTitle = intent.getStringExtra("actionText");

    LogUtils.e(TAG, "  --WebActivity--  " + keyName + "  ---  " + actionID + "  ---  " + tvTitle);
}


private void initView() {
    mWebView = (WebView) findViewById(R.id.sport_web);
    webTitle = (TextView) findViewById(R.id.title_tv);

    progressBar = (ProgressBar) findViewById(R.id.web_progress);
    ((ImageView) findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}

@SuppressLint("JavascriptInterface")
private void initSetting() {
    // 获取webview的设置
    WebSettings settings = mWebView.getSettings();
    settings.setLoadWithOverviewMode(true);
    settings.setBuiltInZoomControls(true);// 设置是否显示放大缩小网页的按钮(wap网页不支持)
    settings.setUseWideViewPort(true);// 设置是否支持双击放大(wap网页不支持)
    settings.setJavaScriptEnabled(true);// 设置是否支持android和网页中js代码的互调
    settings.setDomStorageEnabled(true);

    String cacheDirPath = mContext.getFilesDir().getAbsolutePath() + "cache/";
    settings.setAppCachePath(cacheDirPath);
    settings.setAppCacheMaxSize(20 * 1024 * 1024);
    settings.setAppCacheEnabled(true);

    //如果想要webview支持网页中的alert,比如给weview设置WebChromeClient
    mWebView.setWebChromeClient(new WebChromeClient());

    //js调用android代码的操作
    //设置js可以调用的android的接口对象方法
    //参数1:js调用android的接口对象
    //参数2:js调用Android的接口对象的别名
    mWebView.addJavascriptInterface(new JSAndroidInterface(), "android");

    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            LogUtils.e(TAG, "ProgressChanged  ++  " + newProgress);
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);//设置加载进度
            }
        }
    });

}

@Override
protected void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
}

@Override
protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
}


class JSAndroidInterface {
    @JavascriptInterface
    public void goBack() {
        LogUtils.e(TAG, "222222222-222222222222");
        mContext.finish();
    }

    @JavascriptInterface
    public void gotoUnivSportOrder(final String firstSelectedItem) {
        LogUtils.e(TAG, "----gotoUnivSportOrder---" + firstSelectedItem);
    }
}

}


android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
layout="@layout/title_bar_layout"/>







android:progressDrawable="@drawable/pro_bar_drawable"



    
        
            
        
    



    
        
            
        
    



    
        
            
        
    

你可能感兴趣的:(Android里面WebView加载HTML里面点击按钮调我Android方法)