自定义密码样式(黑点换成¥、%等字符)

本文介绍了一个简单的Android应用程序,该程序实现了密码输入框的明文与密文切换功能。通过两个按钮控制EditText中输入内容的显示状态,即密码形式或明文形式,并提供了清除输入的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制下面代码即可:

布局:

<?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="请输入数据"
        />

    <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);

        //将输入框中的内容变为密码格式
        psd = (Button) findViewById(R.id.password);
        //将密码变为明文
        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) {
                //获取编辑框中的数据内容
                String context = et.getText().toString();
                //将密码变为明文,这里不用setInputType
                et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                //设置光标位置在数据最后
                et.setSelection(context.length());
            }
        });
        psd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取编辑框中的数据内容
                String context = et.getText().toString();
                //将数据变为指定样式的密码
                et.setTransformationMethod(new AsteriskPasswordTransformationMethod());
                //设置光标位置在数据最后
                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) {
                /*
                当在编辑框中输入1的时候,会连续打印0...
                当在编辑框中继续输入2的时候,会连续01...
                不影响功能使用,但是出现原因不知,待解决
                 */
                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
            }
        }
    }

}
简化一下latex的tikzpicture画图描述。\begin{figure}[h] % H参数强制精确定位(需float宏包) \centering % 图形居中 \begin{tikzpicture}[ x={(1cm,0cm)},y={(.1cm,.15cm)}, z={(0cm,1cm)}] % 绘制坐标轴 \draw[->, thick] (-2,0,0) -- (2,0,0) node[below, font=\fontsize{12}{14}\selectfont] {$x$}; \draw[->, thick] (0,-2,0) -- (0,3,0) node[right, font=\fontsize{12}{14}\selectfont] {$y$}; \draw[->, thick] (0,0,-0.25) -- (0,0,2.75) node[above, font=\fontsize{12}{14}\selectfont] {$z$}; % 绘制3x3正方形(xoy平面,蓝色) \draw[blue,very thick] (-1.5,-1.5,0) -- (1.5,-1.5,0) -- (1.5,1.5,0) -- (-1.5,1.5,0) -- cycle; \draw[orange, thick, dashed] (-1.5,0,0) -- (0.301,0,2.399); % 第一条线段 \draw[orange, thick, dashed] (-0.301,0,2.399) -- (1.5,0,0); % 第二条线段 \draw[orange, thick] (0,0,2) -- (0.174,0,1.015); % 第一条线段 \draw[orange, thick] (0,0,2) -- (0.5,0,1.134); % 第二条线段 \node[black, fill=white, inner sep=0.5pt, font=\fontsize{12}{14}\selectfont] at (0.35,0,0.95) {$\theta$}; \draw[orange, thin] plot[domain=-80:-60, samples=50, smooth] ({0.7*cos(\x)}, 0, {2 + 0.7*sin(\x)}); \draw[orange, thick] plot[domain=-127:-53, samples=50, smooth] ({0.25*cos(\x)}, 0, {2 + 0.25*sin(\x)}); \draw[orange, thick] plot[domain=53:127, samples=50, smooth] ({0.25*cos(\x)}, 0, {2 + 0.25*sin(\x)}); \draw[orange, thick] plot[domain=-127:-53, samples=50, smooth] ({0.5*cos(\x)}, 0, {2 + 0.5*sin(\x)}); \draw[orange, thick] plot[domain=53:127, samples=50, smooth] ({0.5*cos(\x)}, 0, {2 + 0.5*sin(\x)}); % 绘制点(1,0,1.5)(xoz平面,红色) \node[circle, fill=red, inner sep=2pt] at (0,0,2) {}; \node[circle, fill=black, inner sep=1pt] at (-0.125,0,1.783) {};%(0.25*cos(-120/180*pi),0,2+0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (0.125,0,1.783) {}; \node[circle, fill=black, inner sep=1pt] at (0.043,0,1.753) {};%(0.25*cos(-100/180*pi),0,2+0.25*sin(-100/180*pi)) \node[circle, fill=black, inner sep=1pt] at (-0.043,0,1.753) {}; %% \node[circle, fill=black, inner sep=1pt] at (0.125,0,2.217) {};%(0.25*cos(-120/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (0.043,0,2.246) {}; \node[circle, fill=black, inner sep=1pt] at (-0.043,0,2.246) {};%(0.25*cos(-100/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (-0.125,0,2.217) {}; %% \node[circle, fill=black, inner sep=1pt] at (-0.25,0,1.567) {};%(0.25*cos(-120/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (-0.087,0,1.508) {}; \node[circle, fill=black, inner sep=1pt] at (0.087,0,1.5076) {};%(0.25*cos(-100/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (0.25,0,1.567) {}; %% \node[circle, fill=black, inner sep=1pt] at (0.25,0,2.433) {};%(0.25*cos(-120/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (0.087,0,2.492) {}; \node[circle, fill=black, inner sep=1pt] at (-0.087,0,2.492) {};%(0.25*cos(-100/180*pi),0,0.25*sin(-120/180*pi)) \node[circle, fill=black, inner sep=1pt] at (-0.25,0,2.433) {}; \end{tikzpicture} \captionsetup{justification=raggedright, singlelinecheck=false} % 题注左对齐 \caption{ Distribution of the DFP and constraint sampling points above the planar array aperture.} % 添加题注 \label{Fig 2-1} % 添加标签(建议使用连字符) \end{figure}
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值