【问题标题】:How can I change the background color of the button when it is pressed/down?按下/按下按钮时如何更改按钮的背景颜色?
【发布时间】:2016-11-04 10:06:17
【问题描述】:

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

我想知道如何为 Xamarin.Forms Android 做到这一点?

我已经有一个自定义渲染器设置,但我需要类似的东西

Control.SetBackgroundColor(Color, ButtonState.Down)

所以它会是向下状态的那种颜色。 Idk,如果有办法为Android做到这一点?

另外,我使用 C# 作为我的视图。

【问题讨论】:

    标签: c# android xamarin xamarin.forms


    【解决方案1】:

    通常您会为此使用 StateListDrawable,您可以在一个 Drawable 中定义每个状态并将其指定为 Button 的背景。

    您可以像这样定义一个 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>
    

    然后您可以将此可绘制对象分配给布局中的按钮:

    <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;
    

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

    【讨论】:

    • Opps,刚刚明白你的意思。还有一个问题,我的可绘制对象可以是颜色而不是图像吗?如果,我的按钮的颜色,我希望它是它所在的当前页面的背景颜色......所以它是一种动态颜色?这是行不通的。谢谢!
    • @Cheesebaron - 看起来您的答案是针对 Xamarin for Android,而不是 Xamarin Forms。这是问题要问的吗?如果是这样,我们肯定需要删除 Xamarin.Forms 标记吗?
    • 他有安卓标签。因此Android的答案。他说他有一个自定义渲染器,因此他可以在自定义渲染器中使用此代码来获得所需的效果。
    【解决方案2】:

    您是否尝试过 Xamarin 论坛链接 here 中的代码。我没有亲自尝试过,但它看起来可以用来做你想做的事情,包括使用动态颜色。也许是这样的(我从内存中写了一些代码,所以如果它不起作用,请告诉我):

    表格:

    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); }
        }
    }
    

    机器人:

    [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;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-07
      • 2023-04-01
      • 2021-02-23
      • 1970-01-01
      • 2023-01-15
      • 2021-02-18
      • 1970-01-01
      • 2019-10-13
      相关资源
      最近更新 更多