键盘重叠与条目Xamarin.forms

问题描述 投票:3回答:5

我有一个带有输入字段的Xaml页面。当我尝试在Xamarin.froms中输入值时键盘重叠输入字段

xaml xamarin xamarin-studio
5个回答
4
投票

检查您的条目是否在ScrollView中


3
投票

这个问题有一个很好的解决方案:

在您的MainPage.xaml:

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

为布局添加帮助器:

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

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

1
投票

Android中有一个已知错误(使用xamarin)...

在Android项目中创建一个新类(使用此名称)

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

然后在MainActivity(OnCreate)中写这个......

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

0
投票

Make sure you did not apply Translations to your views。如果你这样做,你可以像这样暂时撤消它们:

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

聚焦/不聚焦事件是您最接近OnSoftKeyboardAppearing /消失的事件。


-1
投票

这是一个非常常见的问题。

对于IOS,您可以使用此插件(添加到您的IOS项目):https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap

在您的iOS项目调用中(在AppDelegate.cs中):

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

你必须在调用Xamarin.Forms.Init()之后执行此操作。

对于Android:您可以在MainActivity.cs上添加SoftInput设置

[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
    {

...
© www.soinside.com 2019 - 2024. All rights reserved.