1
votes

I have an Entry and a Button:

<StackLayout>
    <CustomViews:ChatEntryView x:Name="ChatEntry" />
    <Button Text="Send" Command="SendCommand"/>
</StackLayout>

What I wanted to achieve here is that when the user starts types something on the Entry control and then presses the button, it should not hide the keyboard (or lose the Entry Focus).

The ChatEntryView here is just actually a custom view that inherits from the Entry control and what I did inside:

1.) Added an Unfocused handler

Unfocused += ChatEntryView_Unfocused;

void ChatEntryView_Unfocused(object sender, FocusEventArgs e)
{
    this.Focus();
}

2.) Tried Handling on PropertyChanged

protected override void OnPropertyChanged(string propertyName = null)
{
    this.Focus();
    base.OnPropertyChanged(propertyName);
}

3.) Tried Handling on PropertyChanging

protected override void OnPropertyChanging(string propertyName = null)
{
    this.Focus();
    base.OnPropertyChanging(propertyName);
}

But all the three methods doesn't seem to work. I was able to do a work around on IOS by making a custom renderer and it's not very neat (by actually interfacing to the Control.ShouldEndEditing on IOS).

But my problem now is on Android, as I don't exactly know how to do this on Android and there's no Control.ShouldEndEditing (the interface on Android) that I can work with.

What happens by using the handlers above is that, the keyboard for the entry view still loses focus and then immediately gets focuses again which is very odd.

The keyboard pushes down (loses focus) and then pushes up (forced focus).

1
As far as I know, using focus can not keep the keyboard show. Because when you click the button the focus of entry will lost, button will get the focus at that time. The keyboard will hide. I think you should overwrite the keyboard behavior.Mike Ma

1 Answers

1
votes

I know it's too late to anwser this question, but it might be helpful for someone else, I added this code to MainActivity, it might not be a neat solution, but works for me:

        private bool _lieAboutCurrentFocus;
    public override bool DispatchTouchEvent(MotionEvent ev)
    {
        var focused = CurrentFocus;
        bool customEntryRendererFocused = focused != null && focused.Parent is CustomEntryRenderer_Droid;

        _lieAboutCurrentFocus = customEntryRendererFocused;
        var result = base.DispatchTouchEvent(ev);
        _lieAboutCurrentFocus = false;

        return result;
    }

    public override View CurrentFocus
    {
        get
        {
            if (_lieAboutCurrentFocus)
            {
                return null;
            }

            return base.CurrentFocus;
        }
    }