带有虚线Android的自定义跨度下划线文本 [英] Custom Span Underline Text with Dotted line Android

查看:146
本文介绍了带有虚线Android的自定义跨度下划线文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想在文本中用点划线或下划线加下划线,以及单击可跨度.

I was tiring to underline a specific word in the text with a dotted or dashed line with and also Clickable Span.

我还没有找到解决方案,请有人帮我.

I haven't found a solution can someone help me, please.

SpannableStringBuilder sb =新的SpannableStringBuilder(文本); 列表listmot = new ArrayList();

SpannableStringBuilder sb = new SpannableStringBuilder(text); List listmot = new ArrayList();

       listmot=db.getAlldef();

       for(int i=0;i<listmot.size();i++)
       {


            String mot = (listmot.get(i)).get_mot();
            final String def = (listmot.get(i)).get_definition();

       Log.v(null, mot);
       Pattern p = Pattern.compile(mot, Pattern.CASE_INSENSITIVE);
       Matcher m = p.matcher(text);
       while (m.find()){


         //  Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/font.ttf");
           //sb.setSpan(new CustomTypefaceSpan("",tf), m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

      sb.setSpan(new ClickableSpan() {

        @Override
        public void onClick(View widget) {
        //  Toast.makeText(getApplicationContext(), def,Toast.LENGTH_LONG).show();



            int[] values = new int[2]; 
            widget.getLocationOnScreen(values);
            Log.d("X & Y",values[0]+" "+values[1]);
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    AndroidSQLiteTutorialActivity.this);
            alertDialog.setMessage(def);
            alertDialog.setCancelable(true);

            AlertDialog alert11 = alertDialog.create();
            alert11.setCanceledOnTouchOutside(true);

            alert11.show();

        }
    }, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

       } 

       }



       textjson.setText(sb);
       textjson.setMovementMethod(LinkMovementMethod.getInstance());

推荐答案

编辑:我已经更新了解决方案.有时,由于某种原因没有调用带有 LineBackgroundSpan 的解决方案中的 drawBackground 方法.新版本一直有效,而且看起来更加清晰.

I have updated solution. Sometimes that drawBackground method in solution with LineBackgroundSpan just haven't been called for no reasons. New version works all the time and looks much clearer.

我遇到了同样的问题,我通过结合以下解决方案解决了这个问题:

I met the same problem and i solved it by combining this solutions:

  • this https://stackoverflow.com/a/21097663/5373069 and
  • this https://stackoverflow.com/a/6104138/5373069

结果代码如下:

private static class DottedUnderlineSpan extends ReplacementSpan {
    private Paint p;
    private int mWidth;
    private String mSpan;

    private float mSpanLength;
    private boolean mLengthIsCached = false;

    public DottedUnderlineSpan(int _color, String _spannedText){
        p = new Paint();
        p.setColor(_color);
        p.setStyle(Paint.Style.STROKE);
        p.setPathEffect(new DashPathEffect(new float[]{mDashPathEffect, mDashPathEffect}, 0));
        p.setStrokeWidth(mStrokeWidth);
        mSpan = _spannedText;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        mWidth = (int) paint.measureText(text, start, end);
        return mWidth;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        canvas.drawText(text, start, end, x, y, paint);
        if(!mLengthIsCached)
            mSpanLength = paint.measureText(mSpan);

        // https://code.google.com/p/android/issues/detail?id=29944
        // canvas.drawLine can't draw dashes when hardware acceleration is enabled,
        // but canvas.drawPath can
        Path path = new Path();
        path.moveTo(x, y + mOffsetY);
        path.lineTo(x + mSpanLength, y + mOffsetY);
        canvas.drawPath(path, this.p);
    }
}

要使您的下划线在所有密度集上的外观都与 dp

To make your underline look the same on all densities set that dimens in dp

mStrokeWidth = context.getResources().getDimension(R.dimen.stroke_width);
mDashPathEffect = context.getResources().getDimension(R.dimen.dash_path_effect);
mOffsetY = context.getResources().getDimension(R.dimen.offset_y);

这是您的使用方式:

  DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(0xFF00FF00, spannedText);
  spannableString.setSpan(dottedUnderlineSpan, startOfText, endOfText, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

并确保您关闭了TextView上的硬件加速功能,该功能会显示下划线范围. https://stackoverflow.com/a/24467362/5373069

And be sure that you turn off hardware acceleration on textView that will show the underline span. https://stackoverflow.com/a/24467362/5373069

编辑:如果您看到改进此解决方案的方法,我将不胜感激.

if you see ways to improve this solution i would appreciate any good points.

这篇关于带有虚线Android的自定义跨度下划线文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆