如何使用TransformationMethod在android系统 [英] how to use TransformationMethod in android

查看:485
本文介绍了如何使用TransformationMethod在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个密码作为输入,编辑文本。
我需要尽快隐藏字符,因为它是typed.so我使用变换方法。
我是新来这个,所以我尝试过这样的事情。

i want to create a edit text with password as input. i need to hide the character as soon as it is typed.so i use Transformation method. I am new to this so i tried something like this

EditText editText=(EditText)findViewById(R.id.et1);
editText.setTransformationMethod(new PasswordTransformationMethod());


private class PasswordTransformationMethod extends Transformation implements TransformationMethod
{

    @Override
    public CharSequence getTransformation(CharSequence arg0, View arg1) 
    {
        // TODO Auto-generated method stub
        return "/";
    }

    @Override
    public void onFocusChanged(View arg0, CharSequence arg1, boolean arg2,
            int arg3, Rect arg4) 
    {
        arg1=getTransformation(arg1, arg0);

    }
}

它抛出

01-03 10:22:35.750: E/AndroidRuntime(2102): java.lang.IndexOutOfBoundsException

所以我失去了这个something.please帮助
三江源。

so am i missing something.please help with this Thankyou.

推荐答案

以上methos我想有很多的错误。

The above methos i tried has lots of error.

这样
我想我应该分享我用转换密码来点方法

so I think i should be sharing the method which i use to convert password to dots

在这样同一个java的文件创建独立的类

Create seperate class in the same jave file like this

public class MyPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;
        public PasswordCharSequence(CharSequence source) {
            mSource = source; 
        }
        public char charAt(int index) {
            return '.'; 
        }
        public int length() {
            return mSource.length(); 
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};

,然后实现这样的

And then implement like this

et2=(EditText)findViewById(R.id.editText2);
et2.setTransformationMethod(new MyPasswordTransformationMethod());

这篇关于如何使用TransformationMethod在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆