当前位置: 移动技术网 > IT编程>移动开发>Android > Android登陆界面实现清除输入框内容和震动效果

Android登陆界面实现清除输入框内容和震动效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

极速复仇高清,王黎雯个人资料,黄宏小品开锁

本文为大家分享android登陆界面实现清除输入框内容和震动效果的全部代码,具体内容如下:

效果图:

主要代码如下

自定义的一个edittext,用于实现有文字的时候显示可以清楚的按钮:

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;

public class clearedittext extends edittext implements view.onfocuschangelistener,textwatcher{

   //删除按钮的引用
  private drawable mcleardrawable;

  //控件是否有焦点
  private boolean hasfoucs;

  public clearedittext(context context) {
    this(context, null);
  }

  public clearedittext(context context, attributeset attrs) {
    // 这里构造方法也很重要,不加这个很多属性不能再xml里面定义
    this(context, attrs, android.r.attr.edittextstyle);
  }

  public clearedittext(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
    init();
  }

   private void init() {
      //获取edittext的drawableright,假如没有设置我们就使用默认的图片
      mcleardrawable = getcompounddrawables()[2];
      if (mcleardrawable == null) {
        // throw new nullpointerexception("you can add drawableright attribute in xml");
        mcleardrawable = getresources().getdrawable(r.drawable.selector_ic_delete);
      }

      //getintrinsicwidth()取得的是drawable在手机上的宽度,所以不同分辨率下获取到的值是不同的,关键所在处
      mcleardrawable.setbounds(0, 0, mcleardrawable.getintrinsicwidth(), mcleardrawable.getintrinsicheight());

      //默认设置隐藏图标
      setcleariconvisible(false);
      //设置焦点改变的监听
      setonfocuschangelistener(this);
      //设置输入框里面内容发生改变的监听
      addtextchangedlistener(this);
    }

    /**
     * 因为我们不能直接给edittext设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在 edittext的宽度 - 图标到控件右边的间距 - 图标的宽度 和
     * edittext的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑
     */
    @override
    public boolean ontouchevent(motionevent event) {
      if (event.getaction() == motionevent.action_up) {
        if (getcompounddrawables()[2] != null) {

          boolean touchable = event.getx() > (getwidth() - gettotalpaddingright())
              && (event.getx() < ((getwidth() - getpaddingright())));

          if (touchable) {
            this.settext("");
          }
        }
      }

      return super.ontouchevent(event);
    }

    /**
     * 当clearedittext焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
     */
    @override
    public void onfocuschange(view v, boolean hasfocus) {
      this.hasfoucs = hasfocus;
      if (hasfocus) {
        setcleariconvisible(gettext().length() > 0);
      } else {
        setcleariconvisible(false);
      }
    }


    /**
     * 设置清除图标的显示与隐藏,调用setcompounddrawables为edittext绘制上去
     * @param visible
     */
    protected void setcleariconvisible(boolean visible) {
      drawable right = visible ? mcleardrawable : null;
      setcompounddrawables(getcompounddrawables()[0],
          getcompounddrawables()[1], right, getcompounddrawables()[3]);
    }


    /**
     * 当输入框里面内容发生变化的时候回调的方法
     */
    @override
    public void ontextchanged(charsequence s, int start, int count,int after) {
      if(hasfoucs){
        setcleariconvisible(s.length() > 0);
      }
    }

    @override
    public void beforetextchanged(charsequence s, int start, int count,int after) {

    }

    @override
    public void aftertextchanged(editable s) {

    }


    /**
     * 设置晃动动画
     */
    public void setshakeanimation(){
      this.setanimation(shakeanimation(5));
    }

    /**
     * 晃动动画
     * @param counts 1秒钟晃动多少下
     * @return
     */
    public static animation shakeanimation(int counts){
      animation translateanimation = new translateanimation(0, 10, 0, 0);
      translateanimation.setinterpolator(new cycleinterpolator(counts));
      translateanimation.setduration(1000);
      return translateanimation;
    }
}

mainactivity.java 主要是弹出一句话表示按钮的点击事件

import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.toast;
import android.app.activity;

public class mainactivity extends activity {

  private button btnlogin;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_login);
    btnlogin = (button) this.findviewbyid(r.id.btnlogin);
  }

  public void login(view view) {
    toast.maketext(this, "登陆", toast.length_long).show();

  }
}

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffffff"
  android:orientation="vertical"
  android:padding="4.0dip" >

  <com.xuliugen.clearedittext.clearedittext
    android:id="@+id/etxtemail"
    style="@style/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="30.0dip"
    android:drawableleft="@drawable/icon_reg_name"
    android:drawablepadding="10.0dip"
    android:hint="使用邮箱登陆" />

  <com.xuliugen.clearedittext.clearedittext
    android:id="@+id/etxtpwd"
    style="@style/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="20.0dip"
    android:drawableleft="@drawable/icon_reg_password"
    android:drawablepadding="10.0dip"
    android:hint="输入登陆密码"
    android:inputtype="textpassword" />

  <button
    android:id="@+id/btnlogin"
    style="@style/biggreenbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="20.0dip"
    android:onclick="login"
    android:text="登陆" />

</linearlayout>

另外还有一些selector文件,图片资源等:
bg_btn_style_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_enabled="false"><shape android:shape="rectangle">
      <corners android:radius="2.0dip" />

      <solid android:color="@color/green_btn_color_disable" />
    </shape></item>
  <item android:state_pressed="true"><shape android:shape="rectangle">
      <corners android:radius="2.0dip" />

      <solid android:color="@color/green_btn_color_pressed" />
    </shape></item>
  <item><shape android:shape="rectangle">
      <corners android:radius="2.0dip" />

      <solid android:color="@color/green_btn_color_normal" />
    </shape></item>

</selector>

bg_edittext_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/input_bar_bg_active" android:state_focused="true"/>
  <item android:drawable="@drawable/input_bar_bg_normal"/>

</selector>

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网