我在xamarin.forms应用程序中的一个页面中有一个条目,该应用程序在平板电脑上无法正常工作。当我单击它时,出现键盘,我可以输入,但除了占位符之外,什么都没有展示。该应用程序可用于其他Android设备,但我的平板电脑未显示输入数据。平板电脑:Galaxy Tab S- Android 5.0电话:Galaxy S6- Android 6.0
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.MyPage"
Title="SomeTitle"
Padding="10, 40, 10, 10">
<Entry Placeholder="Hi"/>
</ContentPage>
在某些三星设备中,您需要编写自定义渲染器并尝试设置文本颜色或背景颜色,然后它应该起作用。
或
尝试XLABS扩展条目
用法:
ExtendedEntry entry = new ExtendedEntry
{
Text = "Dummy Text",
TextColor = Color.Black,
PlaceholderTextColor = Color.Black,
};
pcl代码
using Xamarin.Forms;
namespace VHS.MobileApp.eVita.CustomViews
{
public class ExtendedEntry : Entry
{
/// <summary>
/// The font property
/// </summary>
public static readonly BindableProperty FontProperty =
BindableProperty.Create("Font", typeof(Font), typeof(ExtendedEntry), new Font());
/// <summary>
/// The XAlign property
/// </summary>
public static readonly BindableProperty XAlignProperty =
BindableProperty.Create("XAlign", typeof(TextAlignment), typeof(ExtendedEntry),
TextAlignment.Start);
/// <summary>
/// The HasBorder property
/// </summary>
public static readonly BindableProperty HasBorderProperty =
BindableProperty.Create("HasBorder", typeof(bool), typeof(ExtendedEntry), true);
/// <summary>
/// The PlaceholderTextColor property
/// </summary>
public static readonly BindableProperty PlaceholderTextColorProperty =
BindableProperty.Create("PlaceholderTextColor", typeof(Color), typeof(ExtendedEntry), Color.Default);
/// <summary>
/// The MaxLength property
/// </summary>
public static readonly BindableProperty MaxLengthProperty =
BindableProperty.Create("MaxLength", typeof(int), typeof(ExtendedEntry), int.MaxValue);
/// <summary>
/// Gets or sets the MaxLength
/// </summary>
public int MaxLength
{
get { return (int)this.GetValue(MaxLengthProperty); }
set { this.SetValue(MaxLengthProperty, value); }
}
/// <summary>
/// Gets or sets the Font
/// </summary>
public Font Font
{
get { return (Font)GetValue(FontProperty); }
set { SetValue(FontProperty, value); }
}
/// <summary>
/// Gets or sets the X alignment of the text
/// </summary>
public TextAlignment XAlign
{
get { return (TextAlignment)GetValue(XAlignProperty); }
set { SetValue(XAlignProperty, value); }
}
/// <summary>
/// Gets or sets if the border should be shown or not
/// </summary>
public bool HasBorder
{
get { return (bool)GetValue(HasBorderProperty); }
set { SetValue(HasBorderProperty, value); }
}
/// <summary>
/// Sets color for placeholder text
/// </summary>
public Color PlaceholderTextColor
{
get { return (Color)GetValue(PlaceholderTextColorProperty); }
set { SetValue(PlaceholderTextColorProperty, value); }
}
public static readonly BindableProperty BorderRadiusProperty =
BindableProperty.Create<ExtendedEntry, float>(p => p.BorderRadius, default(float));
public float BorderRadius
{
get { return (float)GetValue(BorderRadiusProperty); }
set { SetValue(BorderRadiusProperty, value); }
}
}
}
droid代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using VHS.MobileApp.eVita.CustomViews;
using Android.Graphics;
using Android.Text.Method;
using Android.Util;
using Color = Xamarin.Forms.Color;
using VHS.MobileApp.eVita.Droid.CustomRenderer;
using System.ComponentModel;
using Android.Text;
[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRenderer))]
namespace VHS.MobileApp.eVita.Droid.CustomRenderer
{
public class ExtendedEntryRenderer : EntryRenderer
{
protected ExtendedEntryRenderer(IntPtr javaReference, JniHandleOwnership transfer)
{
}
public ExtendedEntryRenderer()
: base()
{
}
private const int MinDistance = 10;
/// <summary>
/// Called when [element changed].
/// </summary>
/// <param name="e">The e.</param>
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var view = (ExtendedEntry)Element;
if (Control != null && e.NewElement != null && e.NewElement.IsPassword)
{
Control.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
Control.TransformationMethod = new PasswordTransformationMethod();
this.Control.SetBackgroundColor(Android.Graphics.Color.White);
}
#region SETTING CUSTOM FONT
if (view.FontFamily != null && view.FontFamily != string.Empty)
{
Typeface typeface = Typeface.CreateFromAsset(Forms.Context.Assets, view.FontFamily);
Control.Typeface = typeface;
}
#endregion
// SetFont(view);
SetTextAlignmentWithVerticalFill(view);
SetPlaceholderTextColor(view);
SetMaxLength(view);
}
/// <summary>
/// Handles the touch.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="Android.Views.View.TouchEventArgs"/> instance containing the event data.</param>
/*void HandleTouch(object sender, TouchEventArgs e)
{
var element = (ExtendedEntry)this.Element;
switch (e.Event.Action)
{
case MotionEventActions.Down:
this.downX = e.Event.GetX();
this.downY = e.Event.GetY();
return;
case MotionEventActions.Up:
case MotionEventActions.Cancel:
case MotionEventActions.Move:
this.upX = e.Event.GetX();
this.upY = e.Event.GetY();
float deltaX = this.downX - this.upX;
float deltaY = this.downY - this.upY;
// swipe horizontal?
if (Math.Abs(deltaX) > Math.Abs(deltaY))
{
if (Math.Abs(deltaX) > MinDistance)
{
if (deltaX < 0)
{
element.OnRightSwipe(this, EventArgs.Empty);
return;
}
if (deltaX > 0)
{
element.OnLeftSwipe(this, EventArgs.Empty);
return;
}
}
else
{
Log.Info("ExtendedEntry", "Horizontal Swipe was only " + Math.Abs(deltaX) + " long, need at least " + MinDistance);
return; // We don't consume the event
}
}
// swipe vertical?
// else
// {
// if(Math.abs(deltaY) > MIN_DISTANCE){
// // top or down
// if(deltaY < 0) { this.onDownSwipe(); return true; }
// if(deltaY > 0) { this.onUpSwipe(); return true; }
// }
// else {
// Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
// return false; // We don't consume the event
// }
// }
return;
}
}*/
/// <summary>
/// Handles the <see cref="E:ElementPropertyChanged" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var view = (ExtendedEntry)Element;
this.Control.SetBackgroundColor(Android.Graphics.Color.White);
//if (e.PropertyName == ExtendedEntry.FontProperty.PropertyName)
//{
// SetFont(view);
//}
//else
if (e.PropertyName == ExtendedEntry.XAlignProperty.PropertyName)
{
SetTextAlignmentWithVerticalFill(view);
}
else if (e.PropertyName == ExtendedEntry.HasBorderProperty.PropertyName)
{
//return;
}
else if (e.PropertyName == ExtendedEntry.PlaceholderTextColorProperty.PropertyName)
{
SetPlaceholderTextColor(view);
}
else
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
this.Control.SetBackgroundColor(view.BackgroundColor.ToAndroid());
}
}
}
/// <summary>
/// Sets the text alignment.
/// </summary>
/// <param name="view">The view.</param>
private void SetTextAlignmentWithVerticalFill(ExtendedEntry entry)
{
switch (entry.XAlign)
{
case Xamarin.Forms.TextAlignment.Center:
Control.Gravity = GravityFlags.Center;
break;
case Xamarin.Forms.TextAlignment.End:
Control.Gravity = GravityFlags.End | GravityFlags.CenterVertical;
break;
case Xamarin.Forms.TextAlignment.Start:
Control.Gravity = GravityFlags.Start | GravityFlags.CenterVertical;
break;
}
}
/// <summary>
/// Sets the color of the placeholder text.
/// </summary>
/// <param name="view">The view.</param>
private void SetPlaceholderTextColor(ExtendedEntry view)
{
if (view.PlaceholderTextColor != Color.Default)
{
Control.SetHintTextColor(view.PlaceholderTextColor.ToAndroid());
}
}
/// <summary>
/// Sets the MaxLength characteres.
/// </summary>
/// <param name="view">The view.</param>
private void SetMaxLength(ExtendedEntry view)
{
Control.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(view.MaxLength) });
}
}
}