简体   繁体   中英

Change the text color of Xamarin Forms Prompt Entry

I try to change the text color of the entry from a Xamarin Forms Prompt. I tried giving a default color for the Entry control in the App resource dictionary:

<Style TargetType="Entry">
    <Setter Property="TextColor" Value="White"/>
</Style>

I also tried providing a style in the styles.xml file of the Android project:

<style name="MainTheme" parent="MainTheme.Base">
      <item name="android:colorActivatedHighlight">@android:color/transparent</item>
      <item name="colorControlActivated">#FFFFFF</item>
      <item name="colorControlNormal">#FFFFFF</item>"
      <item name="colorAccent">#FFFFFF</item >
      <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>  
  </style>

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
    <!--buttons-->
    <item name="colorAccent">#ffffff</item>
    <!--title-->
    <item name="android:textColor">#ffffff</item>
    <!--text-->
    <item name="android:textColorPrimary">#ffffff</item>
    <!--selection list-->
    <item name="android:textColorTertiary">#ffffff</item>
    <!--background-->
    <item name="android:background">#000000</item>
  </style>

And yet no success. It just remains black, and I would like to change the default black to other color that would match my theme (green for instance). I don't care if the solution is cross-platform or not, as my project only targets Android for the moment, that's why I tried the styles.xml approach. I guess there is a key for the dictionary of the style, but I just can't find it. Any idea how could I achieve this?

I would go with custom Prompt, which is really simple to create. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestPrompt.MainPage">
    <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Button Text="Display Prompt!"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                Clicked="OnButtonClicked" />
        </StackLayout>


        <ContentView x:Name="popupView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
            <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
                <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">
                    <ContentView Padding="12, 0, 0, 0" HorizontalOptions="StartAndExpand">
                        <Label FontSize="16" FontAttributes="Bold" TextColor="Green" Text="What is your name?" />
                    </ContentView>
                    <Entry TextColor="Green" FontSize="16" VerticalOptions="CenterAndExpand"/>
                    <StackLayout Orientation="Horizontal" VerticalOptions="End">
                        <Button Text="OK" TextColor="Green" FontSize="16"  BackgroundColor="White" TextTransform="None" Clicked="OK_Clicked" />
                        <Button Text="Cancel" TextColor="Green" FontSize="16"  BackgroundColor="White" TextTransform="None" Clicked="Cancel_Clicked" />
                    </StackLayout>
                </StackLayout>
            </StackLayout>
        </ContentView>
    </AbsoluteLayout>
</ContentPage>

In C# you just need to do something like this:

        void OnButtonClicked(object sender, EventArgs args)
        {
            //This will show the pop up
            popupView.IsVisible = true;
        }

        private void OK_Clicked(object sender, EventArgs e)
        {
            popupView.IsVisible = false;
            //Your stuff here
        }

        private void Cancel_Clicked(object sender, EventArgs e)
        {
            popupView.IsVisible = false;
            //Your stuff here
        }

As you can see, you are able to control here everything. This is how this example looks like:

在此处输入图像描述

I think you have to give the TargetType a Name.

for example x:Key="AEntry"

In App.xaml

 <Style x:Key="AEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="Green"/>
    </Style>

In Mainpage.xaml call Style

  <Entry Style="{StaticResource AEntry}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM