键盘重叠与条目Xamarin.froms

时间:2016-11-23 10:08:14

标签: xaml xamarin xamarin-studio

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

时键盘重叠输入字段

5 个答案:

答案 0 :(得分:4)

检查您的条目是否在ScrollView中

答案 1 :(得分: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); };

答案 2 :(得分: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);
            }

答案 3 :(得分: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 / Disappearing的事件。

答案 4 :(得分:-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
    {

...