【问题标题】:SpannableString in XamarinAndroidXamarinAndroid 中的 SpannableString
【发布时间】:2021-03-17 06:21:18
【问题描述】:

我有一个 6 行的“文本视图”。 当我点击每一行时,我想用 Toast 显示每行的编号。 当我使用“跨度”时,它只显示最后一个行号。 如何为 Spans 编写循环以返回所有行号。 还有其他建议吗? 感谢您的帮助。

我的代码:

public class Activity1 : AppCompatActivity
{
    TextView textView;
    int count = 6; //Number of lines
    int next;
    string txt;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        textView = FindViewById<TextView>(Resource.Id.textView1);
        txt = Resources.GetString(Resource.String.test);

        SpannableString ss = new SpannableString(txt);
        var clickableSpan = new MyClickableSpan();
        clickableSpan.Click += v => Toast.MakeText(this, "line : 1", ToastLength.Short).Show();
        ss.SetSpan(clickableSpan, 0, a, SpanTypes.ExclusiveExclusive);
        textView.TextFormatted = ss;
        textView.MovementMethod = new LinkMovementMethod();

        //For all lines
            //var clickableSpan1 = new MyClickableSpan();
            //var clickableSpan2 = new MyClickableSpan();
            //var clickableSpan3 = new MyClickableSpan();
            //var clickableSpan4 = new MyClickableSpan();
            //var clickableSpan5 = new MyClickableSpan();
       ...
    }
    private class MyClickableSpan : ClickableSpan
    {
        public Action<View> Click;

        public override void OnClick(View widget)
        {
            if (Click != null)
                Click(widget);
        }
        public override void UpdateDrawState(TextPaint textPaint)
        {
            textPaint.UnderlineText = false;
        }
    }
}

我的循环代码: From this answer

            for (int i = 0; i < ss.Length(); i = next)
            {
                // find the next span transition
                next = ss.NextSpanTransition(i, ss.Length(), Class.FromType(typeof(CharacterStyle)));
                // get all spans in this range
                int numOfSpans = 0;
                //CharacterStyle[] spans = ss.GetSpans(i, next, Class.FromType(typeof(CharacterStyle))); //return null!!!
                for (int j = 0; j < count; j++)
                {                        
                      numOfSpans++;
                }
                var clickableSpan = new MyClickableSpan();
                clickableSpan.Click += v => Toast.MakeText(this, "line : " + numOfSpans, ToastLength.Short).Show(); //return last number of lines : 6
                ss.SetSpan(clickableSpan, i, next, SpanTypes.ExclusiveExclusive);
                textView.TextFormatted = ss;
                textView.MovementMethod = new LinkMovementMethod();
            }

【问题讨论】:

标签: android for-loop xamarin xamarin.android spannablestring


【解决方案1】:

我不确定你想要达到什么目的,所以请更新你的描述:)

据我所知,当您设置等于ss 时,错误就在这里textView.TextFormatted = ss;,它将覆盖循环的每个步骤中的文本视图的文本。

我给你写了一段代码,textview 有 6 个跨度,当你点击每个跨度时,它会显示跨度的编号。

public class MainActivity : AppCompatActivity
    {
        TextView textView;
        int count = 6; //Number of lines
        string txt;
        List<string> spans;
        int startIndex = 0;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            textView = FindViewById<TextView>(Resource.Id.textView1);
            txt = Resources.GetString(Resource.String.test);

            //Split your txt on the number of spans - count??
            spans = new List<string>()
            {
                "Span 1" + System.Environment.NewLine,
                "Span 2" + System.Environment.NewLine,
                "Span 3" + System.Environment.NewLine,
                "Span 4" + System.Environment.NewLine,
                "Span 5" + System.Environment.NewLine,
                "Span 6" + System.Environment.NewLine
            };

            SpannableStringBuilder spannableString = new SpannableStringBuilder();

            for (int i = 0; i < count; i++)
            {
                ISpannable wordtoSpan = new SpannableString(spans[i]);

                var clickableSpan = new MyClickableSpan(ShowToastForSpan, i);

                spannableString.Append(spans[i], clickableSpan, SpanTypes.ExclusiveExclusive);
            }

            textView.TextFormatted = spannableString;

            textView.MovementMethod = LinkMovementMethod.Instance;
        }

        private void ShowToastForSpan(int spanNumber)
        {
            Toast.MakeText(this, $"Span number {spanNumber + 1} was clicked", ToastLength.Long).Show();
        }
    }

    class MyClickableSpan : ClickableSpan
    {
        private Action<int> _onSpanClicked;
        private int _spanNumber;
        Random rnd = new Random();

        public MyClickableSpan(Action<int> onSpanClicked, int spanNumber)
        {
            _onSpanClicked = onSpanClicked;
            _spanNumber = spanNumber;
        }

        public override void OnClick(View widget)
        {
            _onSpanClicked.Invoke(_spanNumber);
        }

        public override void UpdateDrawState(TextPaint textPaint)
        {
            textPaint.Color = Color.Argb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256));
            textPaint.UnderlineText = false;
        }
    }

【讨论】:

  • 非常感谢.. 如果我扩展“span1”字符串,例如“span 1 span 1 span 1 span 1span 1”并在 60sp 上调整“textView”字体,我将在“textView”中有 4 行" 并通过单击每一行返回相同的数字。我的 txt 字符串包含一个短篇小说,通过改变它的字体,行数也必须改变......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 2016-11-22
  • 1970-01-01
  • 2019-06-16
  • 2013-03-25
  • 1970-01-01
相关资源
最近更新 更多