现在的位置: 首页 > 综合 > 正文

关于Android WebView的那点事儿..

2018年01月16日 ⁄ 综合 ⁄ 共 1570字 ⁄ 字号 评论关闭

1.禁止网页长按弹出文字选择功能:

mWebView.setOnLongClickListener(new WebView.OnLongClickListener() {

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		return true;
	}
			
});

2.只有点击请求的是链接是才会调用,重写此方法返回true表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边。(可重写此方法在跳转网页时修改其标题):

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    //设置页面新标题
	String title = getTitle(url);
	mTitleText.setText(title);
				
    //加载页面
	view.loadUrl(url);
				
	return true;
}

3.在网页中处理按键事件:

@Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        return super.shouldOverrideKeyEvent(view, event);
}

4.点击网页中电话号码或者手机进行拨打电话:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.contains("tel:")) {
	String phone = url.substring(url.indexOf(":") + 1);
	SystemUtil.callPhone(WebViewActivity.this, phone);
	return true;
	}
				
	return true;
}

/**
 * 拨打电话
 * @param context
 * @param num
 */
public static void callPhone(Context context, String num) {
    if(isPhoneNumberValid(num)) {
        //点击数字后立即拨打
//		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num));
		//点击数字后跳转到拨号盘
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + num));
		context.startActivity(intent);
	}
}
	
	
/**
 * 验证电话号码或者手机号码
 * @param phoneNumber
 * @return
 */
public static boolean isPhoneNumberValid(String phoneNumber) {
	boolean isValid = false;
		
	String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|"
			+ "(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|"
			+ "(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
	CharSequence inputStr = phoneNumber;
	Pattern pattern = Pattern.compile(expression);
	Matcher matcher = pattern.matcher(inputStr);
	if(matcher.matches()) {
		isValid = true;
	}
		
	return isValid;
}

抱歉!评论已关闭.