Androidは削除ボタンのEditTextを持っています。
8769 ワード
MainActivityは以下の通りです
package cc.textview5;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
/**
* Demo :
* EditText
*
* :
* http://blog.csdn.net/xiaanming/article/details/11066685
*
* Thank you very much
*/
public class MainActivity extends Activity {
private CleanableEditText mUserNameCleanableEditText;
private CleanableEditText mPassWordCleanableEditText;
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mUserNameCleanableEditText=(CleanableEditText) findViewById(R.id.userNameEditText);
mPassWordCleanableEditText=(CleanableEditText) findViewById(R.id.passwordEditText);
mLoginButton=(Button) findViewById(R.id.loginButton);
mLoginButton.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(mUserNameCleanableEditText.getText())) {
mUserNameCleanableEditText.setShakeAnimation();
Toast.makeText(MainActivity.this, " ", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(mPassWordCleanableEditText.getText())) {
mPassWordCleanableEditText.setShakeAnimation();
Toast.makeText(MainActivity.this, " ", Toast.LENGTH_SHORT).show();
}
}
}
}
Cleeanable EditTextは以下の通りです。
package cc.textview5;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
/**
* clean
*/
public class CleanableEditText extends EditText {
private Drawable mRightDrawable;
private boolean isHasFocus;
public CleanableEditText(Context context) {
super(context);
init();
}
public CleanableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CleanableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
//getCompoundDrawables:
//Returns drawables for the left, top, right, and bottom borders.
Drawable [] drawables=this.getCompoundDrawables();
// right Drawable
// android:drawableRight
mRightDrawable=drawables[2];
//
this.setOnFocusChangeListener(new FocusChangeListenerImpl());
// EditText
this.addTextChangedListener(new TextWatcherImpl());
// clean
setClearDrawableVisible(false);
}
/**
* clean
*
* getWidth():
* event.getX(): ( )
* getTotalPaddingRight():clean
* getPaddingRight():clean
* :
* getWidth() - getTotalPaddingRight() :
* clean
* getWidth() - getPaddingRight() :
* clean
* clean
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean isClean =(event.getX() > (getWidth() - getTotalPaddingRight()))&&
(event.getX() < (getWidth() - getPaddingRight()));
if (isClean) {
setText("");
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
private class FocusChangeListenerImpl implements OnFocusChangeListener{
@Override
public void onFocusChange(View v, boolean hasFocus) {
isHasFocus=hasFocus;
if (isHasFocus) {
boolean isVisible=getText().toString().length()>=1;
setClearDrawableVisible(isVisible);
} else {
setClearDrawableVisible(false);
}
}
}
// clean
private class TextWatcherImpl implements TextWatcher{
@Override
public void afterTextChanged(Editable s) {
boolean isVisible=getText().toString().length()>=1;
setClearDrawableVisible(isVisible);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
}
}
// clean
protected void setClearDrawableVisible(boolean isVisible) {
Drawable rightDrawable;
if (isVisible) {
rightDrawable = mRightDrawable;
} else {
rightDrawable = null;
}
// left, top, right, and bottom
setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],
rightDrawable,getCompoundDrawables()[3]);
}
// ,
public void setShakeAnimation() {
this.setAnimation(shakeAnimation(5));
}
//CycleTimes
public Animation shakeAnimation(int CycleTimes) {
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10);
translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
translateAnimation.setDuration(1000);
return translateAnimation;
}
}
main.xmlは以下の通りです
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dip"
android:paddingRight="20dip"
>
<cc.textview5.CleanableEditText
android:id="@+id/userNameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="username"
android:layout_marginTop="30dip"
android:drawableLeft="@drawable/icon_user"
android:drawableRight="@drawable/clean_selector"
/>
<cc.textview5.CleanableEditText
android:id="@+id/passwordEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="password"
android:password="true"
android:layout_marginTop="100dip"
android:drawableLeft="@drawable/account_icon"
android:drawableRight="@drawable/clean_selector"
/>
<Button
android:id="@+id/loginButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="155dip"
/>
</RelativeLayout>
以上はAndroidに対してです。 削除ボタンを持っているEditTextの資料を整理して、引き続き関連資料を補充します。ありがとうございます。