xamarin.ios中的单选按钮
如何在xamarin.ios本机中创建单选按钮?我们不想使用形式方法xamarin.ios中的单选按钮,xamarin.ios,Xamarin.ios,如何在xamarin.ios本机中创建单选按钮?我们不想使用形式方法 我们尝试使用创建自定义控件,但不起作用。您可以参考以下代码: 这是ViewController.cs: public override void ViewDidLoad () { base.ViewDidLoad (); // Perform any additional setup after loading the view, typically from a nib.
我们尝试使用创建自定义控件,但不起作用。您可以参考以下代码: 这是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 ();
}
}
}
希望它能帮助您。您可以参考以下代码: 这是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 ();
}
}
}
希望它能帮助您。在Xamarin IOS中,单选按钮只有两种选择。您可以使用pickerview,它的作用类似于单选按钮,因为它只允许您选择一个选项。但是,如果您确实想要单选按钮的外观,那么您可以使用WKWebView并以html格式构建表单,然后使用一段代码从webview获取单选按钮状态,并执行您需要完成的任何操作。在Xamarin IOS中,单选按钮只有两个选项。您可以使用pickerview,它的作用类似于单选按钮,因为它只允许您选择一个选项。然而如果您确实想要单选按钮的外观,那么您可以使用WKWebView并以html格式构建表单,然后使用一段代码从webview获取单选按钮状态,并执行您需要完成的任何操作。与Xamarin.iOS配合使用的BEMCheckBox库see for nuget包提供单选按钮功能性:与Xamarin.iOS配合使用的BEMCheckBox库请参见nuget软件包提供单选按钮功能:在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;
}
}
在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;
}
}
谢谢@alanc liu。我花了一些时间修改单选按钮并向其添加所需的事件。但最终还是得到了。谢谢。谢谢@alanc liu。我花了一些时间修改单选按钮并向其添加所需的事件。但最终还是得到了。谢谢,谢谢。易于设置,类似RadioButtonGroup,看起来很棒。我遇到的唯一问题是谢谢你。易于设置,类似RadioButtonGroup,看起来很棒。我遇到的唯一问题