如何在xamarin.ios(native)中创建单选按钮?我们不想使用表单方法。
我们尝试使用创建自定义控件,但它无法正常工作。
答案 0 :(得分:3)
您可以参考此代码:
这是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:
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 :(得分:1)
与Xamarin.iOS一起使用的BEMCheckBox库(请参阅https://github.com/saturdaymp/XPlugins.iOS.BEMCheckBox获取nuget包)提供单选按钮功能:https://github.com/Boris-Em/BEMCheckBox#group--radio-button-functionality
答案 2 :(得分:1)
在iOS中,我们只能通过自定义来实现。我们可以在需要管理按钮上的动作和图像的一门课上讲,这看起来像单选b按钮。 下面是我们如何制作单选按钮的示例:
公共类OoRadioButtonView:OoBaseSelectionView
{
public OoRadioButtonView(bool hasTextField = false) : base(hasTextField)
{
_markSize = 11;
CheckMark = new RoundCornersView()
{
roundedCornerRadius = (_checkMarkSize / 2),
RoundedBottomLeftRadius = true,
RoundedBottomRightRadius = true,
RoundedTopLeftRadius = true,
RoundedTopRightRadius = true,
BackgroundColor = ColorConverter.FromHex(PclColors.Background.LightLightGray),
borderWidth = 2,
borderColor = ColorConverter.FromHex(PclColors.Common.DarkGray),
BorderRadius = (_checkMarkSize / 2)
};
Add(CheckMark);
Mark = new RoundCornersView()
{
roundedCornerRadius = (_markSize / 2),
RoundedBottomLeftRadius = true,
RoundedBottomRightRadius = true,
RoundedTopLeftRadius = true,
RoundedTopRightRadius = true,
BackgroundColor = Colors.MAIN_RED,
Hidden = true
};
Add(Mark);
CreateCheckMarkConstraints();
}
}
下面是我在上面的类中继承的抽象类
公共抽象类OoBaseSelectionView:UIView,INotifyPropertyChanged
{
public bool IsSelected
{
get
{
if (Mark != null)
return !Mark.Hidden;
return false;
}
}
public int Value { get; set; }
public RoundCornersView CheckMark { get; set; }
public UIView Mark { get; set; }
public UILabel OptionLabel { get; set; }
public UILabel SecondaryLabel { get; set; }
public UITextField TextField { get; set; }
public string TextFieldHint
{
get
{
return TextField.Placeholder;
}
set
{
TextField.Placeholder = value;
}
}
string _text;
public string Text
{
get
{
return _text;
}
set
{
_text = value;
TextField.Text = _text;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
}
}
protected float _checkMarkSize;
protected float _markSize;
public event PropertyChangedEventHandler PropertyChanged;
public bool HasTextFiled { get; private set; }
protected OoBaseSelectionView(bool hasTextFiled)
{
HasTextFiled = hasTextFiled;
_checkMarkSize = 22;
CreateViews();
CreateConstraints();
}
void CreateViews()
{
OptionLabel = new UILabel()
{
TextColor = Colors.TEXT_BLACK_COLOR,
Font = UIFont.FromName(Fonts.FONT_NOTO_SANS, 14f),
Lines = 2
};
Add(OptionLabel);
SecondaryLabel = new UILabel()
{
TextColor = Colors.TEXT_BLACK_COLOR,
Font = UIFont.FromName(Fonts.FONT_NOTO_SANS, 14f),
TextAlignment = SettingsManager.IsLTR ? UITextAlignment.Right : UITextAlignment.Left
};
Add(SecondaryLabel);
if (HasTextFiled)
{
TextField = new UITextField();
TextField.Layer.CornerRadius = 10;
TextField.Layer.BorderWidth = 1;
TextField.UserInteractionEnabled = false;
TextField.TextAlignment = UITextAlignment.Center;
TextField.Layer.BorderColor = UIColor.Black.CGColor;
TextField.LeftView = new UIView(new CoreGraphics.CGRect(0, 0, 10, 10));
TextField.LeftViewMode = UITextFieldViewMode.Always;
Add(TextField);
}
}
void CreateConstraints()
{
this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
this.AddConstraints(
OptionLabel.AtTopOf(this, Dimensions.GLOBAL_MARGIN_SMALL),
SecondaryLabel.AtTopOf(this, Dimensions.GLOBAL_MARGIN_SMALL),
SecondaryLabel.AtTrailingOf(this, Dimensions.GLOBAL_MARGIN_MEDIUM)
);
this.AddConstraints(OptionLabel.ToLeadingOf(SecondaryLabel, Dimensions.GLOBAL_MARGIN_EXTRASMALL));
if (HasTextFiled)
{
this.AddConstraints(
TextField.WithSameCenterX(this),
TextField.Below(OptionLabel, Dimensions.GLOBAL_MARGIN_SMALL),
TextField.Width().EqualTo(150),
TextField.Height().EqualTo(35),
TextField.AtBottomOf(this)
);
}
else
{
this.AddConstraints(
OptionLabel.AtBottomOf(this, Dimensions.GLOBAL_MARGIN_SMALL),
SecondaryLabel.AtBottomOf(this, Dimensions.GLOBAL_MARGIN_SMALL)
);
}
}
public void CreateCheckMarkConstraints()
{
this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
this.AddConstraints(
CheckMark.AtLeadingOf(this, Dimensions.GLOBAL_MARGIN_MEDIUM),
CheckMark.WithSameCenterY(OptionLabel),
CheckMark.Height().EqualTo(_checkMarkSize),
CheckMark.Width().EqualTo(_checkMarkSize),
Mark.Height().EqualTo(_markSize),
Mark.Width().EqualTo(_markSize),
Mark.WithSameCenterX(CheckMark),
Mark.WithSameCenterY(CheckMark)
);
if (LangUtils.IsArabic)
this.AddConstraints(OptionLabel.ToLeftOf(CheckMark, Dimensions.GLOBAL_MARGIN_SMALL));
else
this.AddConstraints(OptionLabel.ToRightOf(CheckMark, Dimensions.GLOBAL_MARGIN_SMALL));
}
public void UnSelect()
{
if (Mark != null)
Mark.Hidden = true;
if (HasTextFiled)
{
TextField.UserInteractionEnabled = false;
TextField.Text = null;
}
}
public void Select()
{
if (Mark != null)
Mark.Hidden = false;
if (HasTextFiled)
{
TextField.UserInteractionEnabled = true;
TextField.BecomeFirstResponder();
}
}
public void Toggle()
{
if (Mark != null)
Mark.Hidden = !Mark.Hidden;
}
public void SetOptionLabel(string option)
{
OptionLabel.Text = option;
}
public void SetSecondaryLabel(string secondary)
{
SecondaryLabel.Text = secondary;
}
public string GetTextFieldString()
{
return TextField.Text ?? string.Empty;
}
}
答案 3 :(得分:0)
在Xamarin IOS中,单选按钮只有两个选项。您可以使用选择器视图,其作用类似于单选按钮,因为它只允许您选择一个选项。但是,如果你真的想要单选按钮的外观,你可以使用WKWebView并在html中构建表单,然后有一段代码从webview中获取单选按钮状态,并预先形成你需要完成的任何操作。