[英]Show password button(Unity NGUI / C#)
我需要执行显示用户输入密码的按钮。 我尝试使用按钮更改InputType字段,但这仅适用于密码->标准否(适用于Standrd->密码)。
这是我的按钮脚本
{
GameObject NewPasswordInput;
private UIInput passwordInput;
// Use this for initialization
void Start()
{
NewPasswordInput = GameObject.Find("ActuallyPasswordInput");
}
// Update is called once per frame
void Update () {
passwordInput = GameObject.Find("ActuallyPasswordInput").GetComponent<UIInput>();
passwordInput.UpdateLabel();
}
//
public void Cancel()
{
_stateManager.ChangeScreen(ScreenStateEnum.ProfileEdit);
}
//
public void Confirm()
{
_stateManager.ChangeScreen(ScreenStateEnum.ProfileEdit);
}
public void ShowPassword()
{
if (passwordInput.inputType == UIInput.InputType.Standard) {
passwordInput.inputType = UIInput.InputType.Password;
passwordInput.UpdateLabel();
}
if (passwordInput.inputType == UIInput.InputType.Password){
passwordInput.inputType = UIInput.InputType.Standard;
passwordInput.UpdateLabel();
}
}
}
使用if-else
! 当前两个语句都已执行
public void ShowPassword()
{
if (passwordInput.inputType == UIInput.InputType.Standard)
{
passwordInput.inputType = UIInput.InputType.Password;
passwordInput.UpdateLabel();
}
// after the first statement was executed this will allways be true
// and will revert the change right ahead
if (passwordInput.inputType == UIInput.InputType.Password)
{
passwordInput.inputType = UIInput.InputType.Standard;
passwordInput.UpdateLabel();
}
}
因此无论以前是什么,结果始终是passwordInput.inputType = UIInput.InputType.Standard
。
改为使用
if (passwordInput.inputType == UIInput.InputType.Standard)
{
passwordInput.inputType = UIInput.InputType.Password;
passwordInput.UpdateLabel();
}
// execute the second check only if the frírst condition wasn't met before
else if (passwordInput.inputType == UIInput.InputType.Password)
{
passwordInput.inputType = UIInput.InputType.Standard;
passwordInput.UpdateLabel();
}
为了使阅读更容易,我会做
public void TooglePasswordVisablilty()
{
bool isCurrentlyPassword = passwordInput.inputType == UIInput.InputType.Password;
passwordInput.inputType = isCurrentlyPassword ? UIInput.InputType.Standard : UIInput.InputType.Password;
passwordInput.UpdateLabel();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.