当按钮被按下/按下时,我如何改变它的背景颜色?

本文关键字:改变 背景 颜色 按钮 何改变 | 更新日期: 2023-09-27 18:16:09

我有一个自定义按钮,当按钮状态按下/点击时,我想删除较暗的颜色/阴影/背景色,所以它已经与按钮的背景色相匹配。看起来没有人按它但它仍然有一个click事件附加在它上面。

我想知道如何为Xamarin做到这一点。形式安卓?

我已经有了一个自定义渲染器设置,但是我需要像

这样的东西
Control.SetBackgroundColor(Color, ButtonState.Down)

所以它会是那个向下状态的颜色。我想,是否有办法在Android上做到这一点?

当按钮被按下/按下时,我如何改变它的背景颜色?

通常情况下,你会使用StateListDrawable,在一个Drawable中定义每个状态,并将其指定为按钮的背景。

您可以这样定义一个可绘制的XML,为每个状态使用不同的图像:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed_img" android:state_pressed="true"/>
    <item android:drawable="@drawable/hovered_img" android:state_hovered="true"/>
    <item android:drawable="@drawable/focused_img" android:state_focused="true"/>
    <item android:drawable="@drawable/default_img"/>    
</selector>

然后你可以将这个drawable分配给布局中的按钮:

<Button
    ...
    android:background="@drawable/statelistdrawable" />

你也可以在c#中创建:

var drawable = new StateListDrawable();
drawable.AddState(new[] {Android.Resource.Attribute.StateFocused},
                      Resources.GetDrawable(Resource.Drawable.focused));

然后设置为背景:

var myButton = new Button(this);
myButton.Background = drawable;

状态现在应该反映在按钮上

您是否尝试过Xamarin论坛链接中的代码?我个人没有尝试过,但看起来它可以用来做你想做的事情,包括使用动态颜色。也许是这样的东西(我从内存中写了一些代码,所以让我知道如果它不工作):

形式:

public class FancyButton : Button {
    /// <summary>
    /// The color pressed down color property.
    /// </summary>
    public static readonly BindableProperty PressedDownColorProperty = BindableProperty.Create(nameof(PressedDownColor), typeof(Color), typeof(FancyButton), default(Color));
    /// <summary>
    /// Gets or sets the pressed down color.
    /// </summary>
    /// <value>The color to change to when the button is pressed down string.</value>
    public Color PressedDownColor {
        get { return (Color)GetValue(PressedDownProperty); }
        set { SetValue(PressedDownProperty, value); }
    }
}

Droid:

[assembly: ExportRenderer(typeof(App2.FancyButton), typeof(FancyButtonAndroid))]
namespace App2.Droid {
    public class FancyButtonAndroid : ButtonRenderer {
        private Color _pressedDownColor;
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) {
            base.OnElementChanged(e);
            Android.Widget.Button thisButton = Control as Android.Widget.Button;
            FancyButton formsButton = (FancyButton)Element;
            _pressedDownColor = formsButton.PressedDownColor;
            thisButton.Touch += (object sender, Android.Views.View.TouchEventArgs e2) => {
                if (e2.Event.Action == MotionEventActions.Down) {
                    thisButton.SetBackgroundColor(_pressedDownColor.ToAndroid());
                } else if (e2.Event.Action == MotionEventActions.Up) {
                    thisButton.SetBackgroundColor(Xamarin.Forms.Color.Default.ToAndroid());
                }
            };
        }
        /// <summary>
        /// Raises the element property changed event.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">The event arguments</param>
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
            base.OnElementPropertyChanged(sender, e);
            FancyButton formsButton = Element as FancyButton;
            if(formsButton != null && e.PropertyName == FancyButton.PlaceholderProperty.PropertyName) {
                _pressedDownColor = formsButton.PressedDownColor;
            }
        }
    }
}