自定義抖動的登陸界面

如何打造一個很不平凡的登陸輸入框呢,那就自定義加上一下動畫來完成:


1.先來看看自定義的ClearEditText類

public classClearEditTextextendsEditTextimplementsView.OnFocusChangeListener,TextWatcher {

/**

* 刪除按鈕的引用

*/

privateDrawablemClearDrawable;

privateContextcontext;

/**

* 控件是否有焦點

*/

private booleanhasFocus;

publicClearEditText(Context context) {

this(context,null);

//? ? ? ? super(context);

//? ? ? ? this.context = context;

//? ? ? ? init();

}

publicClearEditText(Context context,AttributeSet attrs){

//這里構造方法也很重要,不加這個很多屬性不能再XML里面定義

this(context, attrs, android.R.attr.editTextStyle);

}

publicClearEditText(Context context, AttributeSet attrs,intdefStyle) {

super(context, attrs, defStyle);

init();

}

private voidinit() {

//獲取EditText的DrawableRight,假如沒有設置我們就使用默認的圖片

mClearDrawable= getCompoundDrawables()[2];

if(mClearDrawable==null) {

mClearDrawable= getResources().getDrawable(R.drawable.delete_selector);

}

mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsicWidth(),mClearDrawable.getIntrinsicHeight());

//默認設置隱藏圖標

setClearIconVisible(false);

//設置焦點改變的監(jiān)聽

setOnFocusChangeListener(this);

//設置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽

addTextChangedListener(this);

}

@Override

public booleanonTouchEvent(MotionEvent event) {

if(mClearDrawable!=null&& event.getAction() == MotionEvent.ACTION_UP) {

intx = (int) event.getX();

//判斷觸摸點是否在水平范圍內(nèi)

booleanisInnerWidth = (x > (getWidth() - getTotalPaddingRight())) &&

(x < (getWidth() - getPaddingRight()));

//獲取刪除圖標的邊界,返回一個Rect對象

Rect rect =mClearDrawable.getBounds();

//獲取刪除圖標的高度

intheight = rect.height();

inty = (int) event.getY();

//計算圖標底部到控件底部的距離

intdistance = (getHeight() - height) /2;

//判斷觸摸點是否在豎直范圍內(nèi)(可能會有點誤差)

//觸摸點的縱坐標在distance到(distance+圖標自身的高度)之內(nèi),則視為點中刪除圖標

booleanisInnerHeight = (y > distance) && (y < (distance + height));

if(isInnerHeight && isInnerWidth) {

this.setText("");

}

}

return super.onTouchEvent(event);

}

/**

* 設置清除圖標的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去

*

*@paramvisible

*/

private voidsetClearIconVisible(booleanvisible) {

Drawable right = visible ?mClearDrawable:null;

setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1],

right, getCompoundDrawables()[3]);

}

/**

* 當ClearEditText焦點發(fā)生變化的時候,判斷里面字符串長度設置清除圖標的顯示與隱藏

*/

@Override

public voidonFocusChange(View v,booleanhasFocus) {

this.hasFocus= hasFocus;

if(hasFocus) {

setClearIconVisible(getText().length() >0);

}else{

setClearIconVisible(false);

}

}

/**

* 當輸入框里面內(nèi)容發(fā)生變化的時候回調(diào)的方法

*/

@Override

public voidonTextChanged(CharSequence text,intstart,intlengthBefore,intlengthAfter) {

if(hasFocus) {

setClearIconVisible(text.length() >0);

}

}

@Override

public voidbeforeTextChanged(CharSequence s,intstart,intcount,intafter) {

}

@Override

public voidafterTextChanged(Editable s) {

}

/**

* 設置晃動動畫

*/

public voidsetShakeAnimation() {

this.setAnimation(shakeAnimation(5));

}

/**

* 晃動動畫

*

*@paramcounts1秒鐘晃動多少下

*@return

*/

public staticAnimation shakeAnimation(intcounts) {

Animation translateAnimation =newTranslateAnimation(0,10,0,0);

translateAnimation.setInterpolator(newCycleInterpolator(counts));

translateAnimation.setDuration(1000);

returntranslateAnimation;

}

}

2.接著來看看主函數(shù)的代碼:

public classMainActivityextendsActivity {

privateToastmToast;

privateButtonmButton;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

finalClearEditText username = (ClearEditText) findViewById(R.id.username);

finalClearEditText password = (ClearEditText) findViewById(R.id.password);

mButton= (Button) findViewById(R.id.login);

mButton.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

if(TextUtils.isEmpty(username.getText())){

//設置晃動

username.setShakeAnimation();

//設置提示

showToast("用戶名不能為空!");

return;

}

if(TextUtils.isEmpty(password.getText())){

password.setShakeAnimation();

showToast("密碼不能為空!");

return;

}

}

});

}

/**

* 顯示Toast消息

*@parammsg

*/

private voidshowToast(String msg) {

if(mToast==null){

mToast= Toast.makeText(this,msg,Toast.LENGTH_SHORT);

}else{

mToast.setText(msg);

}

mToast.show();

}

}

3.主函數(shù)的布局:

xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"

android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

android:background="#95CAE4"

tools:context=".MainActivity">

android:id="@+id/username"

android:layout_marginTop="60dp"

android:background="@drawable/login_edittext_bg"

android:drawableLeft="@drawable/icon_user"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:singleLine="true"

android:drawableRight="@drawable/delete_selector"

android:hint="輸入用戶名"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:id="@+id/password"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:layout_marginTop="10dip"

android:drawableLeft="@drawable/account_icon"

android:hint="輸入密碼"

android:singleLine="true"

android:password="true"

android:drawableRight="@drawable/delete_selector"

android:layout_below="@+id/username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/login_edittext_bg"

/>

android:id="@+id/login"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:background="@drawable/login_button_bg"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="18sp"

android:textColor="@android:color/white"

android:layout_below="@+id/password"

android:layout_marginTop="25dp"

android:text="登錄"

/>

想要完整Demo的可以聯(lián)系我,QQ1223235200

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容