android假设给TextView或EditText的email链接加下划线,并在点击在email连接上能够弹框显示

怎样把textview的一些文字加上背景色:

Spannable str = new SpannableString("#fdsfdfsdfdsfd#");
		Matcher matcher = getEmailPattern().matcher((CharSequence) str);
		while (matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			str.setSpan(new ForegroundColorSpan(0xFF1A5798), start, end,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		textView.setText(str);

假设在TextView上把email链接找出来并加上下划线:

写一个属性类继承ClickableSpan

public class MessageBundleSpan extends ClickableSpan {

	public enum LinkType {
		EMAIL
	}

	private String mText;
	private MainActivity mActivity;

	public MessageBundleSpan(MainActivity activity, String text) {
		if (activity == null) {
			throw new NullPointerException("activity is NULL");
		}

		mActivity = activity;
		mText = text;
	}

	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setColor(mActivity.getResources().getColor(
				R.color.message_bundle_link));
		ds.setUnderlineText(true);
	}

	@Override
	public void onClick(View widget) {
		new AlertDialog.Builder(mActivity).setTitle("Title").setMessage(mText)
				.create().show();
	}

}
<span style="white-space:pre">	</span>TextView textView = (TextView)findViewById(R.id.textview);
		String str = "$#%$%$%[email protected]&*&*[email protected]";
		textView.setText(str);

	    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(textView.getText());

	    applyRegexPattern(textView, stringBuilder, getEmailPattern(), MessageBundleSpan.LinkType.EMAIL);

	    textView.setText(stringBuilder, TextView.BufferType.SPANNABLE);
	    textView.setMovementMethod(LinkMovementMethod.getInstance());
private static Pattern getEmailPattern() {
		if (sharppattern == null)
			sharppattern = Pattern.compile("[\\w[.-]][email protected][\\w[.-]]+\\.[\\w]+");
		return sharppattern;
	}

   private void applyRegexPattern(TextView textView, SpannableStringBuilder stringBuilder, Pattern pattern, MessageBundleSpan.LinkType type) {
	        Matcher matcher = pattern.matcher(textView.getText().toString().toLowerCase());
	        while(matcher.find()) {
	            String text = textView.getText().toString().substring(matcher.start(), matcher.end());
	            stringBuilder.setSpan(new MessageBundleSpan(this, text), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
	        }
   }
   

效果图:

时间: 2024-10-13 22:50:10

android假设给TextView或EditText的email链接加下划线,并在点击在email连接上能够弹框显示的相关文章

android如果给TextView或EditText的email链接加下划线,并在点击在email连接上可以弹框显示

如何把textview的一些文字加上背景色: Spannable str = new SpannableString("#fdsfdfsdfdsfd#"); Matcher matcher = getEmailPattern().matcher((CharSequence) str); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); str.setSpan(new Foregr

android如何在textview或edittext上添加表情

先上效果图: 首先来写一个表情的GridView public class EmotionView extends LinearLayout implements OnItemClickListener { private GridView mGridView; private static final ArrayList<Integer> emotionDisplayList = new ArrayList<Integer>(); public static final Link

android里TextView加下划线的几种方式

如果是在资源文件里: <resources> <string name="hello"><u>phone:0123456</u></string> <string name="app_name">MyLink</string> </resources> 如果是代码里: TextView textView = (TextView)findViewById(R.id.tv_t

设置TextView下划线并响应点击事件(SpannableString)

写Demo程序的时候能表带自定义的数据结构对象吗? --低级程序猿 前情提要:网上介绍TextView+SpannableString的文章真心太长,真心看不懂. ====原文===== 下面是一个20行的完整Demo代码:基本原理是使用一个SpannableString并设置其ClickableSpan来响应点击事件. TextView useInfo = (TextView) findViewById(R.id.info); String url_0_text = "用户协议及隐私条款&qu

Android控件——TextView,EditText

TextView: 显示文本控件 EditText 输入文本框 1.TextView常用属性: 2.EditText常用属性: 设置EditText的android:inputType属性可以限制文本输入类型比如android:inputType="textPassword"为设置输入格式为密码格, android:inputType="phone"为设置输入格式为拨号键盘 3.使用TextView与EditText 布局文件: <LinearLayout x

Android TextView加下划线的几种方式

如果是在资源文件里: <resources> <string name="hello"><u>phone:0123456</u></string> <string name="app_name">MyLink</string> </resources> 如果是代码里: TextView textView = (TextView)findViewById(R.id.tv_t

Android 小技巧-- TextView与EditText 同步显示

方法一.利用View.OnKeyListener"同步"显示 Java代码   EditText  myEdit = (EditText)findViewById(R.id.myEdit); TextView myText = (TextView)findViewById(R.id.myText); myEdit.setOnKeyListener(new EditText.OnClickListener(){ public boolean onKey(View v , int keyC

Android自定义EditText去除边框并添加下划线

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!--注

Android 给TextView的内容 添加跳转无下划线

String url = data.blockChainUrl; //https or http url SpannableStringBuilder builder = new SpannableStringBuilder(""); int start = builder.length(); String content = mContext.getResources().getString(R.string.block_chain_query); //填充字体颜色 builder.