Xamarin Android:在自定义标签控件下划线(使其看起来像条目)

时间:2019-09-27 00:50:24

标签: xamarin xamarin.forms xamarin.android

在我们的应用程序中,我们有一些控件是带有数字的Entry控件,因此我们希望用户能够在其中键入内容。其他控件是诸如“日期”或“选取器”控件之类的弹出选择,我们将其编码为“标签”控件。

为使用户具有与Entry控件相同的一致性,对于iOS,我们在其周围放置了与iOS中的Entry控件相同的框架,例如:

标准进入控件Xaml:

<Entry Grid.Row="0" Grid.Column="1" Keyboard="Numeric" ReturnType="Done" WidthRequest="60" 
        Text="{Binding SocketItem.QuantityPerSys, Converter={StaticResource IntComma}}" 
        TextChanged="OnIntegerEntryChanging" Placeholder="0" 
        AutomationId="SocketQtySysValueEntry" />

用于iOS渲染的模仿项控制:

<Frame Grid.Row="1" Grid.Column="1" AutomationId="SocketDecisionDateEntryFrame">
    <Label Text="{Binding SocketItem.DecisionDate, StringFormat='{0:M/d/yy}', Converter={StaticResource LocalTime}}" 
            HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" AutomationId="SocketDecisionDateValueEntry"/>
    <Frame.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnSocketDateTapped" CommandParameter="DecisionDate" />
    </Frame.GestureRecognizers>
</Frame>

但是,在Android中,Entry控件在它们下面有一行: Android Entry Control

我们要使这些标签控件与Entry控件相同,以便它们保持一致: Label and Entry controls

我的假设是,我必须使用已经设置了自定义渲染器的渲染器,但只带有普通的文本下划线(当然这是我不想要的):

    class UnderlinedLabelRenderer : LabelRenderer
    {
        public UnderlinedLabelRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null && Control is FormsTextView textView)
            {
                textView.PaintFlags |= PaintFlags.UnderlineText;
            }
        }
    }

我已经尝试研究Entry控件的实际Xamarin.Forms渲染器代码,但是我对其并不熟悉,或者不知道该看哪个类来解决它(我将在与此同时)。我假设我必须以某种方式向标签单元格或其他内容添加新的框控件。

我也研究了这个问题:Xamarin Forms Android EntryCell Underline,它与我想做的事情相关但相反。

我还尝试使控件禁用进入控件:

<StackLayout Grid.Row="1" Grid.Column="1" HorizontalOptions="End" AutomationId="OppApplicationValueFrame">
    <controls:ReadonlyEntry Text="{Binding Opportunity.Application}" IsEnabled="False"
           AutomationId="OppApplicationValueEntry"/>
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnApplicationTapped" />
    </StackLayout.GestureRecognizers>
</StackLayout>

但这会产生浅灰色的文本(由于禁用)。然后,我创建了一个自定义渲染器来修复文本:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);

    if (Control != null && Control is FormsEditText editText)
    {
        editText.SetTextColor(Android.Graphics.Color.Gray);
        //editText.SetBackgroundColor(Android.Graphics.Color.Gray);
    }
}

,但是控件上的下划线仍为浅灰色(禁用的颜色)。我参考了有关如何删除下划线的问题:Xamarin Forms Android EntryCell Underline。但是相反,将其设置为另一种颜色会使整个单元格变成灰色。

如何在CustomRenderer中更改下划线的颜色?

任何帮助都是不错的选择,或者是一个开始寻找的地方。

1 个答案:

答案 0 :(得分:0)

我认为最简单的方法是使用条目而不是标签,禁用条目的编辑功能,并在其中添加Salesforce MarketingCloudSdk来处理事件:

TapGestureRecognizers

我认为您不能通过使用Lable的自定义渲染器来实现此目的,而本机TextView不具有此功能。

另一种方法是创建自定义控件,例如视图包含标签和带有黑色背景色的视图(高度= 1)位于标签下。

更新

自定义渲染器中的代码以更改下划线的颜色:

<StackLayout>

    <Entry IsEnabled="False" Text="test" HorizontalTextAlignment="Center" />

    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
    </StackLayout.GestureRecognizers>

</StackLayout>