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



在我们的应用程序中,我们有一些控件是接受数字的入口控件,因此我们希望用户能够键入它们。其他控件是弹出选择,例如日期或选取器控件,我们将其编码为 Label 控件。

为了给用户提供与 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控件下面有一行: 安卓入口控制

我们希望将这些标签控件呈现为与 Entry 控件相同的内容,以便它们保持一致: 标签和条目控件

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

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。但是,与将其设置为另一种颜色相反的操作会使整个单元格变为灰色。

如何更改自定义渲染器中下划线的颜色?

任何帮助都会很棒,或者是一个开始寻找的地方。

我认为实现您要求的最简单方法是使用条目而不是标签,禁用条目的编辑功能并在那里添加一个TapGestureRecognizers来处理事件:

<StackLayout>
<Entry IsEnabled="False" Text="test" HorizontalTextAlignment="Center" />
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
</StackLayout>

而且我认为您无法通过使用 Lable 的自定义渲染器来实现这一点,本机 TextView 没有此功能。

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

更新:

自定义呈现器中用于更改下划线颜色的代码:

[assembly: ExportRenderer(typeof(myEntry), typeof(MyEntryRenderer))]
namespace App557.Android
{
class MyEntryRenderer : EntryRenderer
{
public MyEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
EditText editText = Control as EditText;
editText.Enabled = false;
editText.SetTextColor(global::Android.Graphics.Color.LightGreen);
editText.Background.SetColorFilter(global::Android.Graphics.Color.LightGreen, PorterDuff.Mode.SrcIn);
editText.SetSingleLine(true);
editText.Ellipsize = TruncateAt.End;
}
}
}
}

最新更新