【问题标题】:xamarin entry not workxamarin 条目不起作用
【发布时间】:2017-05-10 21:25:18
【问题描述】:

我的 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>

【问题讨论】:

标签: xamarin xamarin.forms xamarin.forms.entry


【解决方案1】:

在某些三星设备中会发生这种情况,因为您需要编写自定义渲染器并尝试设置文本颜色或背景颜色,然后它应该可以工作。

试试XLabs Extended Entry

【讨论】:

    【解决方案2】:

    试试这个

    用法:

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

    机器人代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多