android - 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!
解决方案
我发现了一个肮脏的可能性,几乎可以满足我的需要
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;
}
}