0

I've a custom Entry Renderer in Android and I've implemented BeforeTextChanged event so that I get the text which is gonna be entered, but it doesn't return that text.

I've implemented in two different ways:

  • First implementing ITextWatcher by the class and then overriding the mentioned event (helping this link):

    void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
    {
    
        var test1 = new string(s?.ToArray());
        var test2 = Control?.Text;
    
        SpannableString spannableString = new SpannableString(s);
        BackgroundColorSpan backgroundSpan = new     BackgroundColorSpan(Color.Blue);
        spannableString.SetSpan(backgroundSpan, start, start + count, 
        SpanTypes.ExclusiveExclusive);
        var test3 = spannableString;
    
    }
    
  • Second way:

    Control.BeforeTextChanged += (sender, args) =>
    {
    
        var test1 = new string(args?.Text.ToArray());
        var entry = sender as EditText;
        var test2 = entry?.Text;
        var test3 = Control?.Text; 
    
    };
    

But none of them will return the text that is about to be entered. What I want is to access that text and only in some circumstances allowing it to be inserted. I don't wanna use Behavior as it doesn't suit my need.

VahidShir
  • 2,066
  • 2
  • 17
  • 27

1 Answers1

0

First way, ITextWatcher:

 class MyEntryRenderer : EntryRenderer
    {
        public MyEntryRenderer() : base()
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
                Control.AddTextChangedListener(new MyTextWatcher());
            }
        }
    }
    class MyTextWatcher :Java.Lang.Object, ITextWatcher
    {

        public void AfterTextChanged(IEditable s)
        {
            Android.Util.Log.Error("lv", "afterTextChanged ---->" + s);

        }

        public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
        {
            Android.Util.Log.Error("lv", "beforeTextChanged ----> s=" + s + "----start=" + start
          + "----after=" + after + "----count" + count);
        }


        public void OnTextChanged(ICharSequence s, int start, int before, int count)
        {
            Android.Util.Log.Error("lv", "onTextChanged ---->s=" + s + "----start=" + start
+ "----before=" + before + "----count" + count);
        }
    }

it works well.

Second way, Control.BeforeTextChanged, it also works well.

it doesn't return that text

The methods you provided in your question, they are all work well in my phone. Please show more codes, so I can help you find where is wrong.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • It didn't work too. Suppose we wanna enter the first character (say "a") into the entry, so BeforeTextChanged must return "a". But it returns an empty value i.e "". – VahidShir Jan 31 '18 at 11:11
  • Hey wait, maybe I'm making a mistake. Can we expect that BeforeTextChanged returns the very text that is gonna be entered??? – VahidShir Jan 31 '18 at 11:15
  • Hi, if there is "a" in your entry, and you enter "b", BeforeTextChanged will return "a". OnTextChanged and AfterTextChanged will return "ab". By `returns the very text`, what do you mean? – Robbit Jan 31 '18 at 12:17
  • What I meant by that is suppose you're gonna enter the char "a" as the first character in the entry. I wanted to get this "a" in BeforeTextChanged event and prevent it from being inserted in the entry. Though I think I was wrong and this event won't give us that! – VahidShir Jan 31 '18 at 12:32
  • Yes, you are right, `BeforeTextChanged` won't return "a", because it doesn't belong to `BeforeTextChanged` event. It belongs to `OnTextChanged` and `AfterTextChanged` events. You need make a demo to test these event. – Robbit Feb 01 '18 at 06:14