国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android自定義密碼樣式 黑點(diǎn)轉(zhuǎn)換成特殊字符

2019-10-22 18:32:38
字體:
供稿:網(wǎng)友

本文為大家分享了Android自定義密碼樣式的制作代碼,黑點(diǎn)換成¥、%等特殊字符,供大家參考,具體內(nèi)容如下

復(fù)制下面代碼即可:

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  >  <EditText    android:id="@+id/et"    android:layout_width="match_parent"    android:layout_height="50dp"    android:layout_marginTop="25dp"    android:hint="請(qǐng)輸入數(shù)據(jù)"    />  <LinearLayout    android:layout_width="match_parent"    android:layout_height="50dp"    android:orientation="horizontal">    <Button      android:id="@+id/password"      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="1"      android:text="密文"/>    <Button      android:id="@+id/show_text"      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_marginLeft="20dp"      android:layout_weight="1"      android:text="明文"/>  </LinearLayout>  <Button    android:id="@+id/clean"    android:layout_width="match_parent"    android:layout_height="50dp"    android:text="清除"/></LinearLayout>

activity:

package com.chen;import android.app.Activity;import android.os.Bundle;import android.text.method.HideReturnsTransformationMethod;import android.text.method.PasswordTransformationMethod;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {  Button psd;  Button show_text;  EditText et;  Button clean;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //將輸入框中的內(nèi)容變?yōu)槊艽a格式    psd = (Button) findViewById(R.id.password);    //將密碼變?yōu)槊魑?   show_text = (Button) findViewById(R.id.show_text);    //清空輸入框    clean = (Button) findViewById(R.id.clean);    et = (EditText) findViewById(R.id.et);    show_text.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        //獲取編輯框中的數(shù)據(jù)內(nèi)容        String context = et.getText().toString();        //將密碼變?yōu)槊魑模@里不用setInputType        et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());        //設(shè)置光標(biāo)位置在數(shù)據(jù)最后        et.setSelection(context.length());      }    });    psd.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        //獲取編輯框中的數(shù)據(jù)內(nèi)容        String context = et.getText().toString();        //將數(shù)據(jù)變?yōu)橹付邮降拿艽a        et.setTransformationMethod(new AsteriskPasswordTransformationMethod());        //設(shè)置光標(biāo)位置在數(shù)據(jù)最后        et.setSelection(context.length());      }    });    clean.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        et.setText("");      }    });  }  private class AsteriskPasswordTransformationMethod 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; // Store char sequence      }      public char charAt(int index) {        /*        當(dāng)在編輯框中輸入1的時(shí)候,會(huì)連續(xù)打印0...        當(dāng)在編輯框中繼續(xù)輸入2的時(shí)候,會(huì)連續(xù)01...        不影響功能使用,但是出現(xiàn)原因不知,待解決         */        System.out.println("-----" + index + "-----");        //這里返回的char,就是密碼的樣式,注意,是char類型的        return '$'; // This is the important part      }      public int length() {        return mSource.length(); // Return default      }      public CharSequence subSequence(int start, int end) {        return mSource.subSequence(start, end); // Return default      }    }  }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安康市| 墨江| 利川市| 韶山市| 揭东县| 福建省| 色达县| 新干县| 阿图什市| 阿荣旗| 伊川县| 西乌珠穆沁旗| 犍为县| 海口市| 沈丘县| 平定县| 吴旗县| 台江县| 临海市| 襄垣县| 汶川县| 屏南县| 旅游| 宜城市| 三门峡市| 永福县| 平塘县| 故城县| 疏附县| 岫岩| 奈曼旗| 平阳县| 新乡市| 沈阳市| 阿坝县| 海晏县| 盐城市| 璧山县| 平武县| 个旧市| 新平|