首页 > 解决方案 > 如何更改 Xamarin 表单中占位符的文本颜色

问题描述

是否可以更改部分占位符的文本颜色。

<Controls:BorderlessEntry Grid.Row="1" Grid.Column="0"
                          x:Name="ABS_ID"
                          Placeholder="Enter Name Here *"/>

我想将星号“*”颜色更改为红色,其余文本将具有默认颜色。

任何帮助表示赞赏!

标签: xamarin.formsplaceholder

解决方案


您可以尝试以下解决方法:

Entry将 a和 a包装Label成 a StackLayout。并StackLayout使用占位符制作类似的条目。

代码是这样的:

MainPage.xaml:

<StackLayout Orientation="Horizontal" HorizontalOptions="Center"
             VerticalOptions="CenterAndExpand">
    <Entry x:Name="ABS_ID" 
           Text="Enter Name Here     "
           HorizontalOptions="Start" 
           Focused="OnEntryFocused" 
           Unfocused="OnEntryUnFocused"/>
    <Label x:Name="ABS_ID2" 
           Text="*" 
           TextColor="Red" 
           HorizontalOptions="StartAndExpand"  
           VerticalTextAlignment="Center" 
           Margin="-20"/>
</StackLayout>

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    bool showPlaceHolder = true;
    public MainPage()
    {
        InitializeComponent();
    }
    void OnEntryFocused(object sender, EventArgs e)
    {
        if (showPlaceHolder) {
            ABS_ID.Text = "";
            ABS_ID2.Text = "";
        }
    }
    void OnEntryUnFocused(object sender, EventArgs e)
    {
        if ((ABS_ID.Text == "") && (ABS_ID2.Text == ""))
        {
            ABS_ID.Text = "Enter Name Here ";
            ABS_ID2.Text = "*";
            showPlaceHolder = true;
        }
        else {
            showPlaceHolder = false;
        }
    }
}

结果是:

结果截图


推荐阅读