[英]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.