ClickableSpan not clickable in custom view (not TextView)

╄→гoц情女王★ 提交于 2019-12-24 01:18:24

问题


In our app we want to improve scroll speed via drawing custom views with text. Problem is ClickableSpan not clicked. For TextView we can use

textView.setMovementMethod(LinkMovementMethod.getInstance());

But what can I do to make link clicking work in custom view?

public class AutoLinkTextView extends View {
    private static final String TAG = "AutoLinkTextView";
    private StaticLayout staticLayout;

    public AutoLinkTextView(Context context) {
        super(context);
        initializeViews();
    }

    public AutoLinkTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeViews();
    }

    public AutoLinkTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initializeViews();
    }

    private void initializeViews() {
        TextPaint textPaint = new TextPaint();
        textPaint.setTextSize(55);
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.GRAY);
        textPaint.linkColor = Color.RED;

        Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");


        URLSpan urlSpan = new URLSpan( "http://google.com" )
        {
            @Override
            public void onClick( View widget )
            {
                Intent i = new Intent( Intent.ACTION_VIEW );
                i.setData( Uri.parse( getURL() ) );
                getContext().startActivity( i );
            }
        };
        wordtoSpan.setSpan(urlSpan, 19, 26, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        staticLayout = new StaticLayout(wordtoSpan, textPaint, 1300, Layout.Alignment.ALIGN_CENTER, 1, 1, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        staticLayout.draw(canvas);
        TextView textView = new TextView();
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), staticLayout.getHeight());
    }

}

回答1:


Try this:

Here is my sample code

 textView.setText(addClickablePart(), TextView.BufferType.SPANNABLE);

         private SpannableStringBuilder addClickablePart() {
    String str = "I know just how to whisper, And I know just how to cry,I know just where to find the answers. Click Me";
                    SpannableStringBuilder ssb = new SpannableStringBuilder(str);
                    final ForegroundColorSpan fcs = new ForegroundColorSpan(ContextCompat.getColor(AppController.getContext(), R.color.dark_green));

                    final String clickString = "Click Me";
                    int idx1 = str.indexOf(clickString);
                    int idx2 = str.length() - 1;

                    ssb.setSpan(new ClickableSpan() {

                        @Override
                        public void onClick(View widget) {
                            Toast.makeText(MainActivity.this, clickString,
                                    Toast.LENGTH_SHORT).show();                        
                        }
                    }, idx1, idx2, 0);

                    ssb.setSpan(fcs, idx1, idx2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

                    return ssb;
                }


来源:https://stackoverflow.com/questions/37158126/clickablespan-not-clickable-in-custom-view-not-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!