Keyboard Overlapping with Entry Xamarin.froms

天涯浪子 提交于 2019-12-10 10:14:30

问题


I have a Xaml page with Entry Field. Keyboard is overlapping Entry Field when I am trying to enter value in Entry in Xamarin.froms


回答1:


Check your Entry is inside a ScrollView




回答2:


There is a good solution for this issue:

At your MainPage.xaml:

//we could use any layout
<StackLayout x:Name="MainStackLayout">...</StackLayout>

Add helper for layouts:

public static class LayoutHelper
    {
        public static void SetLayoutPosition(this Layout layout, bool onFocus, int x = 0, int y = 0, uint length = 50)
        {
            if (onFocus)
            {
                layout.TranslateTo(x, y, length, Easing.Linear);
            }
            else
            {
                layout.TranslateTo(x, y, length, Easing.Linear);
            }
        }
    }

At your constructor in MainPage.xaml.cs:

this.MainStackLayout.Focused += (s, e) => { this.CentralGrid.SetLayoutPosition(onFocus: true, y: -300); };
this.MainStackLayout.Unfocused += (s, e) => { this.CentralGrid.SetLayoutPosition(onFocus: false); };



回答3:


There is a know bug in Android (with xamarin)...

Create a new class in Android project like this (with this name)

public class AndroidBug5497WorkaroundForXamarinAndroid
    {
        private readonly View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;

        public static void assistActivity(Activity activity, IWindowManager windowManager)
        {
            new AndroidBug5497WorkaroundForXamarinAndroid(activity, windowManager);
        }

        private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity, IWindowManager windowManager)
        {

            var softButtonsHeight = getSoftbuttonsbarHeight(windowManager);

            var content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
            mChildOfContent = content.GetChildAt(0);
            var vto = mChildOfContent.ViewTreeObserver;
            vto.GlobalLayout += (sender, e) => possiblyResizeChildOfContent(softButtonsHeight);
            frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
        }

        private void possiblyResizeChildOfContent(int softButtonsHeight)
        {
            var usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious)
            {
                var usableHeightSansKeyboard = mChildOfContent.RootView.Height - softButtonsHeight;
                var heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (heightDifference > (usableHeightSansKeyboard / 4))
                {
                    // keyboard probably just became visible
                    frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference + (softButtonsHeight / 2);
                }
                else
                {
                    // keyboard probably just became hidden
                    frameLayoutParams.Height = usableHeightSansKeyboard;
                }
                mChildOfContent.RequestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }

        private int computeUsableHeight()
        {
            var r = new Rect();
            mChildOfContent.GetWindowVisibleDisplayFrame(r);
            return (r.Bottom - r.Top);
        }

        private int getSoftbuttonsbarHeight(IWindowManager windowManager)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                var metrics = new DisplayMetrics();
                windowManager.DefaultDisplay.GetMetrics(metrics);
                int usableHeight = metrics.HeightPixels;
                windowManager.DefaultDisplay.GetRealMetrics(metrics);
                int realHeight = metrics.HeightPixels;
                if (realHeight > usableHeight)
                    return realHeight - usableHeight;
                else
                    return 0;
            }
            return 0;
        }
    }

And then in the MainActivity (OnCreate) write this...

// Fix the keyboard so it doesn't overlap the grid icons above keyboard etc, and makes Android 5+ work as AdjustResize in Android 4
            Window.SetSoftInputMode(SoftInput.AdjustResize);
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                // Bug in Android 5+, this is an adequate workaround
                AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this, WindowManager);
            }



回答4:


Make sure you did not apply Translations to your views. If you did, you can temporarily undo them like this:

foreach (Entry entry in entries)
{
    entry.Focused += (a, b) =>
    {
        TranslatedView.TranslationY = 0;
    };
    entry.Unfocused += (a, b) =>
    {
        TranslatedView.TranslationY = initialTranslation;
    };
}

The Focused/Unfocused events are the closest you can get to OnSoftKeyboardAppearing/Disappearing.




回答5:


This is a pretty common issue.

For IOS you can use this Plugin (add to your IOS project): https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap

In your iOS project call (in AppDelegate.cs):

Xamarin.Forms.Init();//platform specific init
KeyboardOverlapRenderer.Init ();

You must do this AFTER you call Xamarin.Forms.Init().

For Android: You can add the SoftInput setting on your MainActivity.cs

[Activity(WindowSoftInputMode = SoftInput.AdjustPan, Label = "@string/app_name", Icon = "@drawable/icon", Theme = "@style/SplashActivity", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait) ]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {

...


来源:https://stackoverflow.com/questions/40761370/keyboard-overlapping-with-entry-xamarin-froms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!