Keep Running

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

andori 动画验证必填项

  1. android项目开发过程中,都会碰到必填项的校验,最简单的就是利用Toast对用进行提示,感觉这种提示太不够人性化了,那么今天就来个带动画的,并可以将光标定位到必填项中。
  2. andorid动画Animation,当用户输入不合法时将对用的控件进行动画显示,方便用户更好的定位到相应位置。

相关源码:

 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;


public class BaseActivitys extends Activity
{
    private Animation shakeAction;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.shakeAction = new TranslateAnimation(-3,3,0,0);//设置动画左右跳动
        this.shakeAction.setDuration(100);
        this.shakeAction.setRepeatCount(5);//动画执行5次
    }
    protected void setRequired(View view,String... error){
        view.startAnimation(shakeAction);
        view.setFocusable(true);
        view.requestFocus();//设置光标为选择状态
        view.setFocusableInTouchMode(true);
        if(view instanceof EditText){
            ((EditText) view).setError(error[0]);
        }
    }
    
}

 

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainAct extends BaseActivitys
{
    private EditText requreEdit = null;
    private Button requreBtn = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        requreEdit = (EditText)this.findViewById(R.id.requreEditText);
        requreBtn = (Button)this.findViewById(R.id.requreBtn);
        requreBtn.setOnClickListener(new View.OnClickListener()
        {
            
            public void onClick(View view)
            {
                if(valid())
                {
                    Log.e("ddd","不为空");
                }
            }
        });
    }
    private boolean valid()
    {
        String requreStr = this.requreEdit.getText().toString();
        if(requreStr.equals(""))
        {
            this.setRequired(this.requreEdit, "请填写备注");
            return false;
        }
        return true;
    }
}

 

 

posted on   Keep Running  阅读(724)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示