【发布时间】:2016-11-23 10:08:13
【问题描述】:
我有一个带有输入字段的 Xaml 页面。当我尝试在 Xamarin.froms 的条目中输入值时,键盘与输入字段重叠
【问题讨论】:
-
请发布更多信息。导致您出现问题的图像和 xaml 文件。否则无法提供帮助
-
您能否发布带有屏幕截图的 xaml 代码以便更好地理解。
标签: xaml xamarin xamarin-studio
我有一个带有输入字段的 Xaml 页面。当我尝试在 Xamarin.froms 的条目中输入值时,键盘与输入字段重叠
【问题讨论】:
标签: xaml xamarin xamarin-studio
检查您的条目是否在 ScrollView 内
【讨论】:
这个问题有一个很好的解决方案:
在您的 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); };
【讨论】:
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);
}
【讨论】:
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;
};
}
Focused/Unfocused 事件是最接近 OnSoftKeyboardAppearing/Disappearing 的事件。
【讨论】:
这是一个很常见的问题。
对于 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
{
...
【讨论】: