简体   繁体   English

显示密码按钮(Unity NGUI / C#)

[英]Show password button(Unity NGUI / C#)

I need to do button that show a password that user input. 我需要执行显示用户输入密码的按钮。 I tried change a InputType field with button but this only work for Password->Standard no for Standrd->Password. 我尝试使用按钮更改InputType字段,但这仅适用于密码->标准否(适用于Standrd->密码)。

this is my script for button 这是我的按钮脚本

{
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();

        }
 }
}

use if-else ! 使用if-else Currently both statements are executed 当前两个语句都已执行

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();
    }
}

so the result is allways passwordInput.inputType = UIInput.InputType.Standard no matter what it was before. 因此无论以前是什么,结果始终是passwordInput.inputType = UIInput.InputType.Standard


Instead use 改为使用

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();
}

Or to make it even easier to read I would do 为了使阅读更容易,我会做

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM