Xamarin Android Editext包装文本

时间:2018-05-05 07:17:40

标签: android xamarin android-edittext multiline

我有一个multine edittet控件,需要通过回车获取此控件的文本。问题是我不知道文本的包装位置。 是否有可能获得在控件中看到的格式化文本? 或者是否有一个OnWrapped事件,以便我可以自己追加/ r / n?

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了一种肮脏的可能性,几乎可以满足我的需要

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