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

标签 xamarin.ios

我需要一个有两个单选按钮的 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/

相关文章:

iphone - 使用 Mono Touch 连续滚动 iphone 的scrollView?

mono - 运行 MonoTouch 应用程序时出现 SIGABRT 错误

ios - iPad3 - 将相机拍摄的照片保存到应用程序的文档文件夹中

ios - 跟踪 iOS 分配时,Apple Instruments 停止工作

visual-studio-2010 - Visual Studio 2010 可移植类库不允许我更改目标

xamarin - 在 Xamarin Forms 中实现自定义 Webview

sqlite - Mono 的复制框架

ios - Xamarin Native 链接 Paypal 失败

c# - 将 AVAudioRecorder.ToUrl() 与 MonoTouch 4.0.1 一起使用返回 null

iphone - 使用 monotouch 的 iPhone 中的 Accordion 菜单样式