Xamarin.iOS中的单选按钮是否可以点击?

时间:2017-10-10 16:55:33

标签: xamarin.ios

我需要使用两个单选按钮来查看视图,其中一次只能单击一个。使用用户Alanc Liu在这里发布的答案:Radio button in xamarin.ios我的View Controller看起来是正确的,但我无法弄清楚如何监听点击以将其他单选按钮设置为false。

我尝试过为ViewDidLoad方法添加一个手势识别器,但还没有任何工作要做(我以前只使用故事板来为按钮点击添加方法)。

我的视图控制器:

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 个答案:

答案 0 :(得分:1)

  1. 在MyRadioButton中公开公开的事件,点击单选按钮时调用它。

    var Bitmap: TBitmap; SrcRect: TRectF; DstRect: TRectF; begin Bitmap := TBitmap.CreateFromFile('C:\MyImage.bmp'); try SrcRect := Bitmap.BoundsF; DstRect := SrcRect; DstRect.Width := DstRect.Width / 2; DstRect.Height := DstRect.Height / 2; DstRect.Offset(8, 8); Image1.Bitmap.Canvas.BeginScene; Image1.Bitmap.Canvas.DrawBitmap(Bitmap, SrcRect, DstRect, 100); Image1.Bitmap.Canvas.EndScene; finally Bitmap.Free; end; end; 中的代码:

    MyRadioButton
  2. 处理viewController中的事件

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

    中的代码
    ViewController
  3. 结果:

    enter image description here