- android项目开发过程中,都会碰到必填项的校验,最简单的就是利用Toast对用进行提示,感觉这种提示太不够人性化了,那么今天就来个带动画的,并可以将光标定位到必填项中。
- 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; } }