显示/隐藏按钮上的密码单击Android Xamarin

时间:2016-12-22 01:23:46

标签: c# android xamarin android-button

如何在button点击 Xamarin Android中隐藏和查看密码?

private void _viewPassword_Click(object sender, EventArgs e) {
    EditText _editTextNew = FindViewById<EditText>(Resource.Id.editTxtPwd_signup);
    if (_editTextNew.InputType== Android.Text.InputTypes.TextVariationPassword) {
        _editTextNew.InputType = Android.Text.InputTypes.ClassText;
        _editTextNew.SetSelection(_editTextNew.Text.Length);
    }
    else if(_editTextNew.InputType == Android.Text.InputTypes.ClassText) {
        _editTextNew.InputType = Android.Text.InputTypes.TextVariationPassword;
        _editTextNew.SetSelection(_editTextNew.Text.Length);
    }                             
} 

1 个答案:

答案 0 :(得分:1)

您可以使用TextVariationVisiblePasswordTextVariationPasswordClassText一样使用。但为了确保满足您的条件,我建议使用全局Boolean

private void _viewPassword_Click(object sender, EventArgs e) {
    if (_isPasswordHidden) {
        _editText.InputType = 
            Android.Text.InputTypes.TextVariationVisiblePassword 
            | Android.Text.InputTypes.ClassText;

    } else {
        _editText.InputType = 
             Android.Text.InputTypes.TextVariationPassword 
             | Android.Text.InputTypes.ClassText;
    }

    _isPasswordHidden = !_isPasswordHidden;
}