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類(lèi)型的
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應(yīng)用中使用TabHost組件繼承TabActivity的布局方法
這篇文章主要介紹了Android應(yīng)用中使用TabHost組件繼承TabActivity的布局方法,文中分別介紹了以Activity和以布局文件進(jìn)行布局的方式,需要的朋友可以參考下2016-04-04
Android啟動(dòng)頁(yè)出現(xiàn)白屏、黑屏的解決方案
這篇文章主要給大家介紹了關(guān)于Android啟動(dòng)頁(yè)出現(xiàn)白屏、黑屏的解決方案,這一個(gè)需求是每位Android開(kāi)發(fā)者都需要的,最近發(fā)現(xiàn)了一個(gè)不錯(cuò)的解決方法,所以分享給大家,文中給出了詳細(xì)的介紹,需要的朋友可以參考下。2017-12-12
基于Retrofit2+RxJava2實(shí)現(xiàn)Android App自動(dòng)更新
這篇文章主要為大家詳細(xì)介紹了基于Retrofit2+RxJava2實(shí)現(xiàn)Android App自動(dòng)更新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
android創(chuàng)建和刪除文件夾和文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇android創(chuàng)建和刪除文件夾和文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案詳解
這篇文章主要介紹了Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案,我們需要做的就是提前檢測(cè)好自己的應(yīng)用是否存在隱私合規(guī)問(wèn)題,及時(shí)整改過(guò)來(lái),下面提供Xposed Hook思路去檢測(cè)隱私合規(guī)問(wèn)題,建議有Xposed基礎(chǔ)的童鞋閱讀,需要的朋友可以參考下2022-07-07
android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例
下面小編就為大家?guī)?lái)一篇android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Gradle學(xué)習(xí)教程之部署上傳項(xiàng)目詳解
這篇文章主要給大家介紹了關(guān)于Gradle學(xué)習(xí)教程之部署上傳項(xiàng)目的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
android studio xml文件實(shí)現(xiàn)添加注釋
這篇文章主要介紹了android studio xml文件實(shí)現(xiàn)添加注釋?zhuān)哂泻芎玫膮⒖純r(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

