xamarin.ios 中的单选按钮 [英] Radio button in xamarin.ios

查看:29
本文介绍了xamarin.ios 中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 xamarin.ios(本机)中创建单选按钮?我们不想使用表单方法.

How to create radio button in the xamarin.ios (native)? We don't want to use form approach.

我们尝试使用创建自定义控件,但它不起作用.

We tried using creating custom control but it is not working.

推荐答案

可以参考这段代码:

这是 ViewController.cs:

This is ViewController.cs:

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        MyRadioButton rBtn = new MyRadioButton (new CGPoint (50, 50), "RadioButton");
        this.Add (rBtn);
    }

这是 MyRadioButton.cs:

This is MyRadioButton.cs:

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

希望能帮到你.

这篇关于xamarin.ios 中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆