Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android自定义view之模仿登录界面文本输入框

Android自定义view之模仿登录界面文本输入框

作者头像
计蒙不吃鱼
发布于 2025-06-12 09:55:40
发布于 2025-06-12 09:55:40
10200
代码可运行
举报
文章被收录于专栏:Android开发Android开发
运行总次数:0
代码可运行

前言

考核时间过了才发的哈

效果图

一.分析

1.组合多个控件完成此输入框静态效果 2.hint值上浮下潜动画 3.一些功能

二、步骤

1.自定义一个控件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class MyEditVIew extends RelativeLayout {


    public MyEditVIew(Context context) {
        super(context,null);
    }

    public MyEditVIew(Context context, AttributeSet attrs) {
        super(context, attrs,0);

    }

    public MyEditVIew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

}

2.写一个相似布局(代码在最后)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//代码在最后源码部分

3.将布局打气到view中

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 LayoutInflater.from(context).inflate(R.layout.my_edit_view, this);

4.小提示文字上浮下潜动画

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 //小提示文字出现动画
    private void minTextshow(TextView textView) {
        AnimationSet animationSet = new AnimationSet(true);
        Animation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        Animation alphaAnimation = new AlphaAnimation(0, 1f);
        animationSet.addAnimation(mHiddenAction);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setDuration(300);
        textview.startAnimation(animationSet);
    }

    //小提示文字隐藏动画
    private void minTexthide(TextView textView) {
        AnimationSet animationSet = new AnimationSet(true);
        Animation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mShowAction.setRepeatMode(Animation.REVERSE);
        Animation alphaAnimation = new AlphaAnimation(1f, 0);
        animationSet.addAnimation(mShowAction);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setRepeatMode(Animation.REVERSE);
        animationSet.setDuration(300);
        textview.startAnimation(animationSet);
        CountDownTimer countDownTimer = new CountDownTimer(300, 300) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                textview.setText("");
            }
        }.start();
    }

5.密码加密解密显示

主要代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 //设置文字非加密
 HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
 edittext.setTransformationMethod(method);
 //设置文字加密
 TransformationMethod method = PasswordTransformationMethod.getInstance();
 edittext.setTransformationMethod(method);

6.其他一些小知识点

1.将光标移到最后

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//将光标移到最后
edittext.setSelection(edittext.getText().toString().length());

2.将键盘中的回车和空格去除

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public static void setEditTextInputSpace(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ") || source.toString().contentEquals("\n")) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

3.给自定义view对外提供一个获取值的方法

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 public String getText() {
        return edittext.getText().toString();
    }

7.源码

1.MyEditVIew.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class MyEditVIew extends RelativeLayout {
    private TextView textview;
    private EditText edittext;
    private boolean mtextisshow;     //文字是否显示判断
    private boolean imgisshow;       //图片是否显示判断
    private String hintText;
    private ImageView imageView;
    private ImageView iV_clean;

    public MyEditVIew(Context context) {
        super(context,null);
    }

    public MyEditVIew(Context context, AttributeSet attrs) {
        super(context, attrs,0);
        init(context, attrs);
        setEditTextInputSpace(edittext);
        textAddChanged();
        imageOnClick();

    }

    public MyEditVIew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //打气布局,获取自定义属性的值
    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.my_edit_view, this);
        textview = findViewById(R.id.textview);
        edittext = findViewById(R.id.edittext);
        imageView = findViewById(R.id.imageView);
        iV_clean=findViewById(R.id.iV_clean);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyEditVIew);
        hintText = ta.getString(R.styleable.MyEditVIew_myhintText);
    }
    //文字输入监听以及一些逻辑处理(未优化)
    private void textAddChanged(){
        edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int textSum = s.toString().trim().length();
                if (textSum == 0) {
                    if (mtextisshow == true) {
                        minTexthide(textview);
                        mtextisshow = false;
                        iV_clean.setVisibility(INVISIBLE);
                        edittext.setHint(hintText);
                    }

                } else {
                    if (imgisshow) {
                        //设置文字非加密
                        HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                        edittext.setTransformationMethod(method);
                        edittext.setSelection(edittext.getText().toString().length());
                    } else {
                        //设置文字加密
                        TransformationMethod method = PasswordTransformationMethod.getInstance();
                        edittext.setTransformationMethod(method);
                        //将光标移到最后
                        edittext.setSelection(edittext.getText().toString().length());
                    }
                    if (mtextisshow == false) {
                        textview.setText(hintText);
                        minTextshow(textview);
                        iV_clean.setVisibility(VISIBLE);
                        mtextisshow = true;

                    }

                }
            }
        });
    }
    //两个图片的点击事件,加密,清除文字
    private void imageOnClick(){
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imgisshow) {
                    imageView.setImageResource(R.mipmap.password_show);
                    HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
                    edittext.setTransformationMethod(method);
                    edittext.setSelection(edittext.getText().toString().length());
                    imgisshow = false;
                } else {
                    imageView.setImageResource(R.mipmap.pwd_invisible);
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    edittext.setTransformationMethod(method);
                    edittext.setSelection(edittext.getText().toString().length());
                    imgisshow = true;
                }
            }
        });
        iV_clean.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext.setText("");
            }
        });
    }
    //小提示文字出现动画
    private void minTextshow(TextView textView) {
        AnimationSet animationSet = new AnimationSet(true);
        Animation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        Animation alphaAnimation = new AlphaAnimation(0, 1f);
        animationSet.addAnimation(mHiddenAction);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setDuration(300);
        textview.startAnimation(animationSet);
    }

    //小提示文字隐藏动画
    private void minTexthide(TextView textView) {
        AnimationSet animationSet = new AnimationSet(true);
        Animation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mShowAction.setRepeatMode(Animation.REVERSE);
        Animation alphaAnimation = new AlphaAnimation(1f, 0);
        animationSet.addAnimation(mShowAction);
        animationSet.addAnimation(alphaAnimation);
        animationSet.setRepeatMode(Animation.REVERSE);
        animationSet.setDuration(300);
        textview.startAnimation(animationSet);
        CountDownTimer countDownTimer = new CountDownTimer(300, 300) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                textview.setText("");
            }
        }.start();
    }

    //将键盘中的回车和空格去除
    public static void setEditTextInputSpace(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ") || source.toString().contentEquals("\n")) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

    //提供一个可获取的值
    public String getText() {
        return edittext.getText().toString();
    }
}

2.my_edit_view.xml

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    >
    <TextView
        android:id="@+id/textview"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#b1b1b1"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/edittext"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="密码"
        android:textSize="22sp"
        android:layout_width="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="6dp"
        android:lines="1"
        />
    <ImageView
        android:id="@+id/iV_clean"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/clean"
        android:visibility="invisible"
        android:layout_marginRight="4dp"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/pwd_invisible"
        android:layout_marginRight="16dp"/>
 </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#b1b1b1"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"/>

</LinearLayout>

3.attrs文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <declare-styleable name="MyEditVIew">
        <attr name="myhintText" format="string"/>
    </declare-styleable>

总结

希望对您有所帮助,欢迎各位大佬留言点评

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
Android的View动画
View动画其实就是使ImageView上的图片在隐藏、旋转、缩放、平移通过动画的过程显示。
夜雨飘零
2020/05/06
1.3K0
炫酷旋转菜单
旋转动画实现 效果 关键代码 //枚举类 菜单状态 public enum Status { OPEN, CLOSE } /** * 单击子菜单的回调接口 */ public interface OnSatelliteMenuItemClickListener { void onClick(View view, int pos); } 创建我们的ViewMenu类继承 ViewGroup 类 并实现相应
对话、
2022/02/22
6.6K0
炫酷旋转菜单
安卓滚动字幕以及TextSwitcher、ImageSwitcher使用
项目源码下载:https://github.com/libin7278/TextSwitcher
先知先觉
2019/01/21
2.4K0
Android Animation动画
drawable是图片地址,oneshot=true表示只展示一遍,false表示循环
yechaoa
2022/06/10
7630
Android Animation动画
自定义View(二)-动画- 代码生成View动画
上篇我们介绍了视图动画,说的确切点应该是视图动画中的补间动画(Tween Animation),关于逐帧动画(Frame Animation)用法更简单,这里先不做介绍后期再自定义View的使用会用到,到时候讲解。这篇我们来一起学习将上篇用XML实现的动画用试着用代码来生成,毕竟有些时候我们是用代码来控制动画的。
g小志
2018/09/11
6270
自定义View(二)-动画- 代码生成View动画
animation rotate_canvas scale
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/04
6660
animation rotate_canvas scale
一起来做个拜年App吧!
---- 下载apk试用 密码: wjep 去github看源码 ---- 前言 马上就要过年了, 做一个App来送祝福是不错的哦, 这里我考虑用ViewPager来做, 大家可以考虑用其它的试试
sean_yang
2018/09/04
6860
一起来做个拜年App吧!
Android动画之View Animation
Android的View animation由四种类型组成:alpha、scale、translate、rotate
码客说
2019/10/22
1.5K0
Android动画系列(2)—补间动画
补充: 除了在XML中指定android:layoutAnimation,还可以通过LayoutAnimationController来实现。
八归少年
2022/06/29
7930
Android动画基础 | 概述、逐帧动画、视图动画
或者给<animation-list>添加android:oneshot="true"属性,也可实现:
凌川江雪
2019/05/14
4.2K0
Android动画基础 | 概述、逐帧动画、视图动画
1.viewpager
ViewPager是android扩展包v4包中的类,这个类可以让用户左右切换当前的view。 ViewPager类直接继承了ViewGroup类,所以它是一个容器类,可以在其中添加其他的view类。
六月的雨
2018/05/14
8440
android scaleanimation动画,Animation之ScaleAnimation(缩放动画片)「建议收藏」
缩放的意思就是对图片或者文字等进行扩大或缩小。下面开始编写代码,相关重要属性参数的解释都在代码中。
全栈程序员站长
2022/11/04
5480
android 随机云标签(圆形)
下面是实现的效果图: 这个适合用于选择 用户的一些兴趣标签,个性名片等。 代码: Activity package com.dyl.cloudtags; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle
再见孙悟空_
2023/02/10
1.9K0
android 随机云标签(圆形)
Android自定义组合控件---教你如何自定义下拉刷新和左滑删除
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/80122918
Hankkin
2018/09/06
1.5K0
Android自定义组合控件---教你如何自定义下拉刷新和左滑删除
Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」
首发:http://blog.csdn.net/harvic880925/article/details/40117115
全栈程序员站长
2022/11/07
3K0
Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」
相关推荐
Android的View动画
更多 >
加入讨论
的问答专区 >
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    本文部分代码块支持一键运行,欢迎体验
    本文部分代码块支持一键运行,欢迎体验