美文网首页
Android里面WebView加载HTML里面点击按钮调我An

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

作者: 自然之秋 | 来源:发表于2017-07-08 10:38 被阅读36次

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

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/title_bar_layout"/>

<ProgressBar
    android:id="@+id/web_progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="5dip"
    android:indeterminateOnly="false"
    android:max="100"
    android:background="@drawable/pro_bar_bg"
    android:progressDrawable="@drawable/pro_bar_drawable" >
</ProgressBar>

<WebView
    android:id="@+id/sport_web"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center">

</WebView>

</LinearLayout>

android:progressDrawable="@drawable/pro_bar_drawable"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <clip>
        <shape android:shape="rectangle">
            <solid android:color="#939090"/>
        </shape>
    </clip>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <gradient
                android:startColor="#85bcf2"
                android:centerColor="#48a0f7"
                android:endColor="#1383f4"
                />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#4da2f7"
                android:centerColor="#0267cc"
                android:endColor="#01118e"
                />
        </shape>
    </clip>
</item>

</layer-list>

相关文章

网友评论

      本文标题:Android里面WebView加载HTML里面点击按钮调我An

      本文链接:https://www.haomeiwen.com/subject/vmmdhxtx.html