13.单选框UIRadioButton类
UIRadioButton和CheckBox的唯一区别是左侧图像不同,所以只需从CheckBox继承即可,只不过在构造函数中需要改变使用的图像。
单选框使用的图像如下:
单选框代码非常简单:
namespace StunEngine.SceneNodes.UI
...{
/**//// <summary>
/// 单选框控件
/// </summary>
public class UIRadioButton:UICheckBox
...{
/**//// <summary>
/// 创建一个单选框控件
/// </summary>
public UIRadioButton(StunXnaGE engine, Scene setScene)
: base(engine, setScene,"RadioButton", "Textures/UI/UIRadioButton")
...{ }
}
}
但在实际应用中,单选框通常都是成组应用的,所以需要新建一个RadiuButtonGroup类,用以管理多个RadiuButton。
RadiuButtonGroup类的主要功能是:
- 依次排列每个radioButton;
- 当选择其中一个radioButton时要将它的Checked属性设为true,将其余的radioButton的Checked属性设为false,并引发ChangeSelection事件。
RadiuButtonGroup类代码如下:
namespace StunEngine.SceneNodes.UI
...{
/**//// <summary>
/// radioButton组
/// </summary>
public class UIRadioButtonGroup:UISceneNode
...{
/**//// <summary>
/// 单选框数组
/// </summary>
UIRadioButton[] radioButtons;
/**//// <summary>
/// 各radioButton之间的垂直间距。
/// </summary>
float verticalSpace = 10.0f;
/**//// <summary>
/// 被选中的单选框索引,初始时为-1,即没有单选框被选中。
/// </summary>
int selectedIndex = -1;
/**//// <summary>
/// 创建一组单选框
/// </summary>
/// <param name="engine">引擎</param>
/// <param name="setScene">控件所属的scene</param>
/// <param name="setPosition">2D屏幕位置</param>
/// <param name="radiobutton">单选框数组</param>
public UIRadioButtonGroup(StunXnaGE engine, Scene setScene, Vector2 setPosition, UIRadioButton[] radiobuttons)
: base(engine,setScene ,setPosition ,null)
...{
//UIRadioButtonGroup没有焦点
this.TabStop = false;
this.radioButtons = radiobuttons;
//向下依次放置各个radioButton
float radioButtonY = 0;
for (int i = 0; i < radiobuttons.Length; i++)
...{
radiobuttons[i].CheckedChanged+=new EventHandler(OnCheckedChanged);
radiobuttons[i].CanUncheck = false;
if (radiobuttons[i].Checked == true)
selectedIndex = i;
radiobuttons[i].Position =new Vector2 (setPosition.X, setPosition.Y + radioButtonY);
radioButtonY += radiobuttons[i].Size.Y + verticalSpace;
}
}
属性#region 属性
/**//// <summary>
/// 获取或设置各radioButton之间的垂直间距,当此间距发生改变时需要重新调整各radioButton的位置。
/// </summary>
public float VerticalSpace
...{
get ...{ return verticalSpace; }
set
...{
verticalSpace = value;
ResetRadioButtons();
}
}
/**//// <summary>
/// 获取或设置被选中的单选框,返回选中单选框上的文字,或根据设置的文字选中单选框
/// </summary>
public string Selected
...{
get
...{
//遍历单选框数组
for (int i = 0; i < radioButtons.Length; i++)
//如果找到第一个为选中状态的单选框,则返回这个单选框的文字
if (radioButtons[i].Checked == true)
return radioButtons[i].Text;
//否则返回""
return "";
}
set
...{
//遍历单选框数组
for (int i = 0; i < radioButtons.Length; i++)
//如果单选框的文字为设定的文字,则将这个单选框的Checked属性设为true
if (radioButtons[i].Text.ToLower() == value.ToLower())
radioButtons[i].Checked = true;
//将其他单选框的Checked设为false
else
radioButtons[i].Checked = false;
}
}
#endregion
事件相关处理程序#region 事件相关处理程序
/**//// <summary>
/// 选中项改变时引发的事件
/// </summary>
public event EventHandler ChangeSelection;
/**//// <summary>
/// 选中项改变时引发的事件的处理程序
/// </summary>
private void OnCheckedChanged(object obj, EventArgs e)
...{
UIRadioButton radiobutton = (UIRadioButton)obj;
//将选中的单选框索引重置为-1
int checkIndex = -1;
//遍历单选框数组找到选中的单选框的索引
for (int i = 0; i < this.radioButtons.Length; i++)
...{
if (this.radioButtons[i] == radiobutton)
...{
checkIndex = i;
break;
}
}
//如果当前被选中的单选框索引不等于前面选中的单选框索引,则引发ChangeSelection事件
if(checkIndex != selectedIndex)
if (ChangeSelection != null)
ChangeSelection(radiobutton.Text, null);
//重新设置选中的单选框索引
selectedIndex = checkIndex;
//将其余单选框的Checked属性设为false
for (int i = 0; i < this.radioButtons.Length; i++)
if (i != checkIndex)
this.radioButtons[i].Checked = false;
}
/**//// <summary>
/// 如果2D位置发生改变则重新放置各radioButton的位置
/// </summary>
protected override void OnLocationChanged()
...{
ResetRadioButtons();
base.OnLocationChanged();
}
/**//// <summary>
/// 切换每个radioButton的IsVisible属性
/// </summary>
/// <param name="e"></param>
protected override void OnVisibleChanged(EventArgs e)
...{
for (int i = 0; i < radioButtons.Length; i++)
radioButtons[i].IsVisible = !radioButtons[i].IsVisible;
base.OnVisibleChanged(e);
}
/**//// <summary>
/// 重新设置每个单选框的位置
/// </summary>
private void ResetRadioButtons()
...{
//将radioButton的竖直位置初始化为0
float radioButtonY = 0;
for (int i = 0; i < this.radioButtons.Length; i++)
...{
//向下依次放置各个radioButton
radioButtons[i].Position = new Vector2(position.X, position.Y + radioButtonY);
radioButtonY += radioButtons[i].Size.Y + verticalSpace;
}
}
#endregion
单元测试#region 单元测试
#if DEBUG
/**//// <summary>
/// 测试UIRadioButtonGroup类
/// </summary>
public static void TestUIRadioButtonGroup()
...{
UIRadioButton[] radioButtons = null;
UIRadioButtonGroup radioGroup=null;
//显示相关信息
UILabel lblMessage = null;
TestGame.Start("测试UIRadioButtonGroup类,可按数字1,2,3键,Right、Down键,空格键",
delegate
...{
radioButtons = new UIRadioButton[3];
radioButtons[0]=new UIRadioButton(TestGame.engine, TestGame.scene);
radioButtons[0].Text = "First";
radioButtons[0].Checked = true;
TestGame.scene.AddNode(radioButtons [0]);
radioButtons[1] = new UIRadioButton(TestGame.engine, TestGame.scene);
radioButtons[1].Text = "Second";
TestGame.scene.AddNode(radioButtons[1]);
radioButtons[2] = new UIRadioButton(TestGame.engine, TestGame.scene);
radioButtons[2].Text = "Third";
TestGame.scene.AddNode(radioButtons[2]);
lblMessage = new UILabel(TestGame.engine, TestGame.scene);
lblMessage.Position = new Vector2(100, 250);
TestGame.scene.AddNode(lblMessage);
radioGroup = new UIRadioButtonGroup(TestGame.engine, TestGame.scene, new Vector2(100,100), radioButtons);
//关闭相机控制器
TestGame.scene.fpsCamCtrl.Enabled = false;
},
delegate
...{
//显示被选中的单选框上的文字
lblMessage.Text = "You Select RadioButton:"+radioGroup.Selected;
//按1,2,3切换选中的单选框
if (Input.KeyboardKeyJustPressed (Keys.D1 ))
radioGroup.Selected = "First";
if (Input.KeyboardKeyJustPressed(Keys.D2))
radioGroup.Selected = "Second";
if (Input.KeyboardKeyJustPressed(Keys.D3))
radioGroup.Selected = "Third";
//按Right键移动控件
if (Input.KeyboardRightJustPressed)
radioGroup.Position += new Vector2(5, 5);
//按Downt键增加各radioButton间距
if (Input.KeyboardDownJustPressed)
radioGroup.VerticalSpace += 2;
//按空格键切换控件的显示与否
if(Input .KeyboardSpaceJustPressed )
radioGroup .IsVisible =!radioGroup .IsVisible ;
}
);
}
#endif
#endregion
}
}
单元测试截图如下:
发布时间:2010/1/20 下午4:42:39 阅读次数:7017
2006 - 2024,推荐分辨率 1024*768 以上,推荐浏览器 Chrome、Edge 等现代浏览器,截止 2021 年 12 月 5 日的访问次数:1872 万 9823。
站长邮箱