簡體   English   中英

Xamarin Android Editext包裝文字

[英]Xamarin Android Editext wrapped text

我有一個multine edittet控件,需要使用回車符來獲取此控件的文本。 問題是我不知道文本在哪里包裝。 是否有可能獲得控件中所見格式的文本? 還是有一個OnWrapped事件,以便我可以自己附加/ r / n?

謝謝!

我發現一個骯臟的可能性幾乎可以滿足我的需求

 public sealed class CustomEditText : EditText
    {

        private int _prevLineCounter = 1;

        public static bool DelIsPressed { get; set; }




        private CustomEditText(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {


        }


        public CustomEditText(Context context) : base(context)
        {
            BeforeTextChanged += CustomEditText_BeforeTextChanged;
            SetOnKeyListener(new KeyListner());
        }



        private void CustomEditText_BeforeTextChanged(object sender, TextChangedEventArgs e)
        {
            // not loaded yet
            if (LineCount == 0)
                return;

            if (DelIsPressed)
            {
                DelIsPressed = false;
                return;
            }

            // text got wrapped
            if (_prevLineCounter < LineCount)
            {
                _prevLineCounter = LineCount;

                // find last space an enter carriage return 
                int spaceIndex = Text.LastIndexOf(' ');

                // just one long string
                if (spaceIndex == -1)
                    spaceIndex = Text.Length - 2;

                Text = Text.Insert(spaceIndex, "\r\n");

                // set cursor to the end
                SetSelection(Text.Length);


            }
            _prevLineCounter = LineCount;
        }



        public CustomEditText(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs,
            defStyleAttr)
        {
        }

        public CustomEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context,
            attrs, defStyleAttr, defStyleRes)
        {
        }
    }

    public class KeyListner: Java.Lang.Object, View.IOnKeyListener
    {

        public bool OnKey(View v, Keycode keyCode, KeyEvent e)
        {
            CustomEditText.DelIsPressed = e.KeyCode == Keycode.Del;
            return false;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM