简体   繁体   中英

Xamarin Android Editext wrapped text

I have a multine edittet control and need to get the text of this control with a carriage return. The problem is that i do not know where the text is wrapped. Is ther a possibility to get the text formatted as seen in the control? Or is there a OnWrapped event so that i can append then the /r/n on my own?

Thank you!

I found a dirty possibility which does nearly what I need

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM