1. 程式人生 > >仿支付寶/微信的password輸入框效果GridPasswordView解析

仿支付寶/微信的password輸入框效果GridPasswordView解析

arp 主類 center 大小 str .get fcm android def

仿支付寶/微信的password輸入框效果GridPasswordView解析,把一些設置和一些關鍵的地方列了出來,方便大家使用,可能能夠省一部分的時間,也算是自己的積累吧。


1.password框能夠輸入的類型PasswordType

public enum PasswordType {
    NUMBER, TEXT, TEXTVISIBLE, TEXTWEB;
}

這裏能夠自行加入類型,然後在GridPasswordView中改動

    @Override
    public void setPasswordType(PasswordType passwordType) {
        boolean visible = getPassWordVisibility();
        int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD;
        switch (passwordType) {

            case TEXT:
                inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
                break;

            case TEXTVISIBLE:
                inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
                break;

            case TEXTWEB:
                inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD;
                break;
        }

        for (TextView textView : viewArr)
            textView.setInputType(inputType);

        setPasswordVisibility(visible);
    }


2.PasswordView中的抽象方法的作用

interface PasswordView {

	//獲取password
    String getPassWord();

    //清空password
    void clearPassword();

    //設置password
    void setPassword(String password);

    //設置password可見性
    void setPasswordVisibility(boolean visible);

    //password可見性的切換
    void togglePasswordVisibility();

    //設置password改變監聽
    void setOnPasswordChangedListener(GridPasswordView.OnPasswordChangedListener listener);
    
    //設置password類型
    void setPasswordType(PasswordType passwordType);
}

3.GridPasswordView中還有些方法沒有去操作。有須要能夠自己去寫

setBackground(Drawable)
setBackgroundColor(int)
setBackgroundResource(int)
setBackgroundDrawable(Drawable)
這個類是View的主類。能夠通過這個類去滿足你的需求


4.在GridPasswordView中設置框框顏色和背景顏色(綠色和紅色)

private static final int DEFAULT_LINECOLOR = 0xaa61cea8;
private static final int DEFAULT_GRIDCOLOR = 0xffe66961;


5.設置文本的大小和顏色(40px和藍色)

private static final int DEFAULT_TEXTSIZE = 40;
textColor = ColorStateList.valueOf(getResources().getColor(android.R.color.holo_blue_light));

6.在gridpasswordview_styles.xml中設置了樣式,大家依據須要更改

    <style name="GridPasswordView.TextView">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:background">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_weight">1</item>
        <item name="android:singleLine">true</item>
    </style>

    <style name="GridPasswordView.EditText" parent="GridPasswordView.TextView">
        <item name="android:cursorVisible">false</item>
    </style>

    <style name="GridPasswordView.Divider">
        <item name="android:layout_width">1dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_marginTop">1dp</item>
        <item name="android:layout_marginBottom">1dp</item>
    </style>

7.在gridpasswordview_attr.xml設置屬性值

    <declare-styleable name="gridPasswordView">

        <attr name="textColor" format="color|reference" />
        <attr name="textSize" format="dimension" />

        <attr name="lineColor" format="color" />
        <attr name="gridColor" format="color" />
        <attr name="lineWidth" format="dimension" />

        <attr name="passwordLength" format="integer" />
        <attr name="passwordTransformation" format="string" />

        <attr name="passwordType" format="enum">
            <enum name="numberPassword" value="0" />
            <enum name="textPassword" value="1" />
            <enum name="textVisiblePassword" value="2" />
            <enum name="textWebPassword" value="3" />
        </attr>

    </declare-styleable>


8.效果圖

技術分享

源代碼下載地址;http://download.csdn.net/detail/pcaxb/8980873

仿支付寶/微信的password輸入框效果GridPasswordView解析