Android自定义view之模仿登录界面文本输入框(华为云APP)

简介: 本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。

前言

考核时间过了才发的哈

老规矩最后有源码

效果图

1.gif

一.分析

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

二、步骤

1.自定义一个控件

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.写一个相似布局(代码在最后)

2.png

//代码在最后源码部分

3.将布局打气到view中

 LayoutInflater.from(context).inflate(R.layout.my_edit_view, this);

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

 //小提示文字出现动画
    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.密码加密解密显示

主要代码

 //设置文字非加密
 HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
 edittext.setTransformationMethod(method);
 //设置文字加密
 TransformationMethod method = PasswordTransformationMethod.getInstance();
 edittext.setTransformationMethod(method);

6.其他一些小知识点

1.将光标移到最后

//将光标移到最后
edittext.setSelection(edittext.getText().toString().length());

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

    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对外提供一个获取值的方法

 public String getText() {
   
        return edittext.getText().toString();
    }

7.源码

1.MyEditVIew.java

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

<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文件

    <declare-styleable name="MyEditVIew">
        <attr name="myhintText" format="string"/>
    </declare-styleable>

总结

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

相关文章
|
10月前
|
移动开发 小程序 Android开发
基于 uni-app 开发的废品回收类多端应用功能与界面说明
本文将对一款基于 uni-app 开发的废品回收类多端应用,从多端支持范围、核心功能模块及部分界面展示进行客观说明,相关资源信息也将一并呈现。
292 0
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
520 65
Android自定义view之网易云推荐歌单界面
|
9月前
|
人工智能 小程序 搜索推荐
【一步步开发AI运动APP】十二、自定义扩展新运动项目2
本文介绍如何基于uni-app运动识别插件实现“双手并举”自定义扩展运动,涵盖动作拆解、姿态检测规则构建及运动分析器代码实现,助力开发者打造个性化AI运动APP。
|
12月前
|
存储 Android开发 数据安全/隐私保护
Thanox安卓系统增加工具下载,管理、阻止、限制后台每个APP运行情况
Thanox是一款Android系统管理工具,专注于权限、后台启动及运行管理。支持应用冻结、系统优化、UI自定义和模块管理,基于Xposed框架开发,安全可靠且开源免费,兼容Android 6.0及以上版本。
1373 4
|
Android开发 开发者
Android企业级实战-界面篇-3
本文是《Android企业级实战-界面篇》系列的第三篇,主要介绍分割线和条形跳转框的实现方法,二者常用于设置和个人中心界面。文章通过具体代码示例展示了如何实现这两种UI组件,并提供了效果图。实现前需准备`dimens.xml`、`ids.xml`、`colors.xml`等文件,部分资源可参考系列第一、二篇文章。代码中详细说明了布局文件的配置,如分割线的样式定义和条形跳转框的组件组合,帮助开发者快速上手并应用于实际项目中。
163 1
《仿盒马》app开发技术分享-- 自定义标题栏&商品详情初探(9)
上一节我们实现了顶部toolbar的地址选择,会员码展示,首页的静态页面就先告一段落,这节我们来实现商品列表item的点击传值、自定义标题栏。
142 0
|
XML Android开发 数据格式
Android企业级实战-界面篇-2
本文为《Android企业级实战-界面篇》系列第二篇,主要介绍三个UI模块的实现:用户资料模块、关注与粉丝统计模块以及喜欢和收藏功能模块。通过详细的XML代码展示布局设计,包括dimens、ids、colors配置文件的使用,帮助开发者快速构建美观且功能齐全的界面。文章结合实际效果图,便于理解和应用。建议配合第一篇文章内容学习,以获取完整工具类支持。
194 0
|
算法 Java Android开发
Android企业级实战-界面篇-1
本文详细介绍了Android企业级开发中界面实现的过程,涵盖效果展示、实现前准备及代码实现。作者通过自身经历分享了Android开发经验,并提供了`dimens.xml`、`ids.xml`、`colors.xml`和`strings.xml`等配置文件内容,帮助开发者快速构建规范化的UI布局。文章以一个具体的用户消息界面为例,展示了如何使用线性布局(LinearLayout)和相对布局(RelativeLayout)实现功能模块排列,并附带注意事项及使用方法,适合初学者和进阶开发者参考学习。
280 0
|
XML Java Android开发
14. 【Android教程】文本输入框 EditText
14. 【Android教程】文本输入框 EditText
1874 2
|
数据安全/隐私保护 Android开发