gpt4 book ai didi

xamarin.ios - Xamarin.iOS 中的单选按钮听点击?

转载 作者:行者123 更新时间:2023-12-04 02:01:49 25 4
gpt4 key购买 nike

我需要一个有两个单选按钮的 View ,一次只能点击一个。使用用户 Alanc Liu 在此处发布的答案:Radio button in xamarin.ios我的 View Controller 看起来是正确的,但我不知道如何监听点击以将另一个单选按钮设置为 false。

我试过向 ViewDidLoad 方法添加手势识别器,但还没有任何效果(我以前主要只是使用 Storyboard 来为按钮点击添加方法)。

我的 View Controller :

public partial class VerifyViewController : UIViewController
{
public VerifyViewController (IntPtr handle) : base (handle)
{
}

public override void ViewDidLoad()
{
base.ViewDidLoad();

MyRadioButton tBtn = new MyRadioButton(new CGPoint(100, 300), "TEXT PHONE");
MyRadioButton eBtn = new MyRadioButton(new CGPoint(100, 375), "EMAIL");

this.Add(tBtn);
this.Add(eBtn);
}
}

和他的单选按钮类:

public class MyRadioButton : UIView
{
private CircleView circleView;
private UILabel lbTitle;

public bool State {
get {
return circleView.State;
}
set {
circleView.State = value;
}
}

public MyRadioButton (CGPoint pt,string title)
{
this.Frame = new CGRect (pt, new CGSize (150, 30));
circleView = new CircleView (new CGRect(0, 0, 30, 30));
lbTitle = new UILabel (new CGRect (30, 0, 120, 30));
lbTitle.Text = title;
lbTitle.TextAlignment = UITextAlignment.Center;
this.AddSubview (circleView);
this.AddSubview (lbTitle);
this.BackgroundColor = UIColor.FromRGBA(1,0,0,0.3f);

UITapGestureRecognizer tapGR = new UITapGestureRecognizer (() => {
State = !State;
});
this.AddGestureRecognizer (tapGR);
}
}

class CircleView : UIView
{
private bool state = false;
public bool State {
get {
return state;
}
set {
state = value;
this.SetNeedsDisplay ();
}
}

public CircleView (CGRect frame)
{
this.BackgroundColor = UIColor.Clear;
this.Frame = frame;
}

public override void Draw (CoreGraphics.CGRect rect)
{
CGContext con = UIGraphics.GetCurrentContext ();

float padding = 5;
con.AddEllipseInRect (new CGRect (padding, padding, rect.Width - 2 * padding, rect.Height - 2 * padding));
con.StrokePath ();

if (state) {
float insidePadding = 8;
con.AddEllipseInRect (new CGRect (insidePadding, insidePadding, rect.Width - 2 * insidePadding, rect.Height - 2 * insidePadding));
con.FillPath ();
}
}
}

最佳答案

  1. 在 MyRadioButton 中公开一个公共(public)事件,当我们点击单选按钮时调用它。

    MyRadioButton 中的代码:

    //define the event inside MyRadioButton
    public delegate void TapHandler(MyRadioButton sender);
    public event TapHandler Tap;

    //call it in MyRadioButton(CGPoint pt, string title)
    UITapGestureRecognizer tapGR = new UITapGestureRecognizer(() => {
    State = !State;
    Tap(this);
    });
  2. 在您的 viewController 中处理事件

    ViewController中的代码

    MyRadioButton tBtn = new MyRadioButton(new CGPoint(100, 300), "TEXT PHONE");
    MyRadioButton eBtn = new MyRadioButton(new CGPoint(100, 375), "EMAIL");

    this.Add(tBtn);
    this.Add(eBtn);

    tBtn.Tap += Btn_Tap;
    eBtn.Tap += Btn_Tap;
    // set the default selection
    Btn_Tap(tBtn);

    MyRadioButton PreviousButton;
    private void Btn_Tap(MyRadioButton sender)
    {
    if(PreviousButton != null)
    {
    //set previous to false
    PreviousButton.State = false;
    }
    //set current to true
    sender.State = true;
    //assign current to previous
    PreviousButton = sender;
    }

结果:

enter image description here

关于xamarin.ios - Xamarin.iOS 中的单选按钮听点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46672043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com