即时通讯中,聊天信息如果有链接则设置下划线,并可点击进入相应链接

</pre><pre name="code" class="html"><pre name="code" class="html"><span style="font-size:18px;">/**
 * 
 * @param msg : 聊天信息
 * @param textview 显示内容的组件
 * @return
 */
	private boolean isUrl(String msg, TextView textview) {
		textview.setText("");
		String HTTP = "http://";
		String HTTPS = "https://";
		String WWW = "www.";
		if(msg.contains(HTTP)||msg.contains(HTTPS)||msg.contains(WWW)){
			String head = www;
			String txt = "";
			String[] urls = msg.split(head);
			//根据www拆分字符串,如将啦啦啦http://www.baidu.com拆分成{啦啦啦http://,baidu.com哈哈}
			String frontText = urls[0];//链接前文字:啦啦啦http://
			if(frontText.endsWith(HTTP)){
				txt = HTTP;
				frontText = frontText.substring(0, frontText.length()-HTTP.length()); </span>
<span style="font-size:18px;"> <span style="white-space:pre">				</span>//去掉http://, frontText=啦啦啦<span style="white-space:pre">	</span>
			}
			if(frontText.endsWith(HTTPS)){
				txt = HTTPS;
				frontText = frontText.substring(0, frontText.length()-HTTPS.length());
			}
			textview.setText(frontText);
			String lastText = "";
			String newLastText = "";
			String nextHead = "";
			int flag = 0;
			for (int i = 1; i < urls.length; i++) {
				String url = urls[i];
				int k = -1;
				int url_length = url.length();
				String txtUrl = head + url; //txtUrl= www.baidu.com哈哈
				for (int j = 0; j < url_length; j++) { 
					char c = url.charAt(j);
					if ((c >= 0x4e00) && (c <= 0x9fbb)) {//找到中文字符:哈哈
						k = j;//中文字符所在的位置
						break;
					}
				}
				if (k != -1) {//如果有中文字符
					lastText = url.substring(k, url_length);//lastText=哈哈
//					Log.e("-----url------", url.substring(0, k));
					txtUrl = head + url.substring(0, k);//txtUrl=www.baidu.com
				}
				if(i == 1){
					txtUrl = txt + txtUrl; //txtUrl=http://www.baidu.com
				}
				
				if(lastText.endsWith(HTTP)){
					flag = i+1;
					nextHead = HTTP;
					lastText = lastText.substring(0, lastText.length()-HTTP.length());
				}
				if(lastText.endsWith(HTTPS)){
					flag = i+1;
					nextHead = HTTPS;
					lastText = lastText.substring(0, lastText.length()-HTTPS.length());
				}
				if(flag == i){
					txtUrl = nextHead + txtUrl;
				}
				String reg = "(.*)weitainet.com/(shop/(\\d*)/index|goods/detail/(\\d*)).htm(.*)";
				if(url.matches(reg)){
					Pattern p0 = Pattern.compile(reg);
					Matcher m0 = p0.matcher(url);
					while (m0.find()) {
						if(url.contains("shop")){
							long shopId = Long.parseLong(m0.group(3));
							setTextClick(txtUrl, textview, lastText, 2, shopId);
						}else{
							long goodsId = Long.parseLong(m0.group(4));
							setTextClick(txtUrl, textview, lastText, 1, goodsId);
						}
					}
				}else{
//					Log.e("---------------", url);
					String regString = "weitainet.com";
					if(url.contains(regString)){
						setTextClick(txtUrl, textview, lastText, 3, 0l);
					}else{ 
						setTextClick(txtUrl, textview, lastText, 4, 0l);
					}
				}
			}
			return true;//含有链接
		}else{
			return false;
		}
		
	}

</span>
<span style="font-size:18px;">//设置文字的点击事件:
	private void setTextClick(final String url,TextView textview,String lastText,final int type,final Long id) {
		
		SpannableString urlText = new SpannableString(url);
		urlText.setSpan(new ClickableSpan() {
					@Override
					public void updateDrawState(TextPaint ds) {
						ds.setColor(Color.BLUE);//字体变蓝色
						super.updateDrawState(ds);
					}

					@Override
					public void onClick(View widget) {
						switch (type) {
						case 1:
							Intent intent = new Intent(mContext,Goods.class);
							intent.putExtra("goodsid",id);
							mContext.startActivity(intent);
							break;
						case 2:
							Intent intent2 = new Intent(mContext,Store.class);
							intent2.putExtra("storeid",id);
							mContext.startActivity(intent2);
							break;
						case 3:
							Intent intent3 = new Intent(mContext,MainActivity.class);
							mContext.startActivity(intent3);
							break;
						case 4:
							// 使用浏览器打开链接
//							Log.e("---------url-----------", url);
							Dialog dialog = new AlertDialog.Builder(mContext)
							.setIcon(R.drawable.ic_launcher)
							.setTitle("提示")
							.setMessage("确定打开外部浏览器?")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									String url0 = url;
									if(url.startsWith("www.")){
										url0 = "http://" + url;
									}							
									Intent intent4 = new Intent(Intent.ACTION_VIEW);
									Uri uri = Uri.parse(url0);
									intent4.setData(uri);
									mContext.startActivity(intent4);
								}

							})
							.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.dismiss();
								}
							})
							.create();
							dialog.show();
							break;
						default:
							break;
						}
					}

				}, 0, url.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		textview.append(urlText);
		textview.append(lastText);
		textview.setMovementMethod(LinkMovementMethod
				.getInstance());// 开始响应点击事件
	}
</span>



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值