【问题标题】:ClickableSpan not clickable in custom view (not TextView)ClickableSpan 在自定义视图中不可点击(不是 TextView)
【发布时间】:2016-09-06 14:07:30
【问题描述】:

在我们的应用程序中,我们希望通过使用文本绘制自定义视图来提高滚动速度。问题是 ClickableSpan 没有被点击。对于 TextView,我们可以使用

textView.setMovementMethod(LinkMovementMethod.getInstance());

但是我该怎么做才能使链接点击在自定义视图中起作用?

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());
    }

}

【问题讨论】:

  • 您需要在自定义视图中覆盖 onTouchEvent,请参阅此answer

标签: android android-custom-view clickablespan


【解决方案1】:

试试这个:

这是我的示例代码

 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;
                }

【讨论】:

  • @RafaelMuhamedzyanov 哦,我明白了,这段代码对我很有用。
  • 我设法通过使用 onTouchEvent 并手动处理 MotionEvent 操作来完成此操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
相关资源
最近更新 更多