Android自定義密碼樣式 黑點(diǎn)轉(zhuǎn)換成特殊字符
本文為大家分享了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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例
本篇文章主要介紹了android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
基于Flutter實(shí)現(xiàn)動(dòng)態(tài)高斯模糊的流程步驟
一個(gè)App加上高斯模糊會(huì)形成一種高級(jí)的感覺,本文將介紹如何制作一個(gè)根據(jù)背景內(nèi)容來動(dòng)態(tài)高斯模糊,文中有詳細(xì)的代碼實(shí)現(xiàn)步驟,代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11
Android中通過MediaStore獲取音樂文件信息方法
這篇文章主要介紹了Android中通過MediaStore獲取音樂文件信息方法,本文講解了獲取歌曲的名稱、歌曲的專輯名、歌曲的歌手名、歌曲文件的全路徑、歌曲文件的名稱、歌曲文件的發(fā)行日期等音樂文件信息的方法,需要的朋友可以參考下2015-04-04
Android ListView優(yōu)化之提高android應(yīng)用效率
android listview優(yōu)化做的好是提高androoid應(yīng)用效率的前提條件,本文給大家介紹Android ListView優(yōu)化之提高android應(yīng)用效率,對(duì)android listview優(yōu)化相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01

