如何像EditText控件一样延迟显示自定义密码字符

时间:2019-08-09 03:19:49

标签: xamarin xamarin.android android-edittext

我通过继承EditText控件创建了一个自定义控件。我的要求是显示自定义密码字符,并且在键入输入类型时,键入时应有一些类似于EditText的延迟。我已经实现了自定义密码字符显示,但是我无法满足第二个要求。我尝试使用 PostDelay 实现第二个要求。键入单个字符时,在更改为密码字符之前可能会有一些延迟,但是快速键入多个字符时,我无法达到类似于编辑文本控件的要求。我将示例附加在以下链接中:https://github.com/Eswaran17392/CustomSamples/blob/master/CustomEditbox.zip。任何人,请建议我如何实现类似于EditText的延迟。为了应用延迟,我使用了以下代码片段:

if(ShowPasswordDelay)
{
Java.Lang.Runnable runnable = new Java.Lang.Runnable(() =>
                    {
                        this.Text = value;
                    });
                    if (handler != null)
                    {
                        handler.RemoveCallbacks(runnable);
                        handler = null;
                        this.Text = value;            
}

if (handler == null)
handler = new Handler();
handler.PostDelayed(runnable, 1000);
}
else
{
     this.Text = value;
}

1 个答案:

答案 0 :(得分:0)

以下是自定义密码字符的示例:

1。创建 PasswordCharSequence 类:

class PasswordCharSequence : Java.Lang.Object, ICharSequence
{
    private ICharSequence mSource;

    public PasswordCharSequence(ICharSequence mSource)
    {
        this.mSource = mSource;
    }
    public char CharAt(int index)
    {
        return '*'; // here is your custom password character
    }
    public IEnumerator<char> GetEnumerator()
    {
        return mSource.GetEnumerator();
    }
    public int Length()
    {
        return mSource.Length();
    }

    public ICharSequence SubSequenceFormatted(int start, int end)
    {
        return mSource.SubSequenceFormatted(start,end);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return mSource.GetEnumerator();
    }
}

2.create CustomPasswordTransformationMethod 类:

class CustomPasswordTransformationMethod : PasswordTransformationMethod
    {
        public override ICharSequence GetTransformationFormatted(ICharSequence source, View view)

3.set EditText如:

EditText edit = FindViewById<EditText>(Resource.Id.edit);
edit.TransformationMethod = new CustomPasswordTransformationMethod();