Android登陆界面实现-支持输入框清楚和震动效果功能
演示效果
主要代码例如以下
自己定义的一个EditText。用于实现有文字的时候显示能够清楚的button:
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{
//删除button的引用
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 主要是弹出一句话表示button的点击事件
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="输入登录password"
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登陆界面实现-支持输入框清楚和震动效果功能的更多相关文章
- android 登陆界面
LoginActivity.java package com.example.ruian; import android.app.Activity; import android.app.AlertD ...
- Ubuntu 12.04 64bit 配置完android 5.0编译环境后出现“could not write bytes: Broken pipe.”而无法进入输入帐号密码的登陆界面
Ubuntu 12.04 64bit 配置完android 5.0编译环境后出现“could not write bytes: Broken pipe.”而无法进入输入帐号密码的登陆界面.上网问了问百 ...
- Android 仿QQ微信开场导航以及登陆界面
相信大家对于微信等社交应用的UI界面已经都很熟悉了,该UI最值得借鉴的莫过于第一次使用的时候一些列产品介绍的图片,可以左右滑动浏览,最后进入应 用,这一效果适用于多种项目中,相信今后开发应用一定会用得 ...
- 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...
- Android仿QQ微信开场导航以及登陆界面
相信大家对于微信等社交应用的UI界面已经都很熟悉了,该UI最值得借鉴的莫过于第一次使用的时候一些列产品介绍的图片,可以左右滑动浏览,最后 进入应用,这一效果适用于多种项目中,相信今后开发应用一定会用得 ...
- Android UI组件----用相对布局RelativeLayout做一个登陆界面
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Android 随着输入框控件的清除功能ClearEditText,抄IOS输入框
今天给大家带来一个非常有用的小控件ClearEditText,就是在Android系统的输入框右边增加一个小图标,点击小图标能够清除输入框里面的内容,IOS上面直接设置某个属性就能够实现这一功能.可是 ...
- 一个网页登陆界面写30多个测试Case——测试之道
转自博文:http://www.cnblogs.com/I-am-Betty/p/3566411.html 具体需求: 有一个登陆页面, (假如上面有2个textbox, 一个提交按钮. 请针对这个页 ...
- alertDialog创建登陆界面,判断用户输入
alertDialog创建登陆界面,需要获取用户输入的用户名和密码,获取控件对象的时候不能像主布局文件那样获得, 需要在onClickListener中获取,代码如下: public boolean ...
随机推荐
- windows的消息传递--消息盒子(超详细EM_UNDO等消息)
使用delphi的消息机制可以方便操作后台,其中重要的就是sendmessage()函数.下面讲解一下这个函数 function SendMessage(hWnd: HWND; Msg: UINT; ...
- Linux下sed,awk,grep,cut,find学习笔记
awk awk是一种程序语言,对文档资料的处理具有很强的功能.awk擅长从格式化报文或从一个大的文本文件中抽取数据. awk的命令格式为: awk [-F filed-separator] “comm ...
- Google的Java经常使用类库 Guava
Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包括很多 Google 核心的 Java 经常使用库. 1. 基本工具 [Basic utilities] 让使用Java ...
- ASP.NET - 网页重定向 Response.Redirect()
在网页中使用重定向,意思就是在网站中的某一个页面跳转到另一个页面. Response.Redirect(~/abc.aspx); 使用“~”的作用是可以从任意位置跳转. 如果没有“~”,那么跳转的时候 ...
- JavaScript移除数组元素
//数组移除长度方法 var array=[]; array[0]="张三"; array[1]="李四"; array[2]="王五"; ...
- TCP/IP笔记 二.网络层(2)——ICMP,RIP,OSPF,BGP
1. ICMP ICMP (Internet Control Message Protocol) 作用:提高 IP 数据报交付成功的机会. 1.1 特点 ICMP 允许主机或路由器报告差错情况和提供有 ...
- iOS:获取图片Alpha图片
-(void)createImages { // Load the alpha image, which is just the same Ship.png image used in the cli ...
- 暂停和屏蔽右键网页中的Flash
如何暂停网页中的Flash?原理很简单,就是屏蔽Flash的消息即可.屏蔽右键也可以通过此方法 直接贴代码吧,加了注释,很容易就能懂了 新建工程,加一个WebBrowser,再加两个按钮.Flash ...
- Dom4j SAXReader Constructors
Dom4j读取xml:eg1: package xml; import java.io.File; import org.dom4j.DocumentException; import org.dom ...
- win7下让程序默认以管理员身份运行
在win7中用自己写的程序读取MBR时,突然提示无法对磁盘进行操作,而在xp下并没有这个问题:最后点右键以管理员身份运行才可以正常运行.于是想办法让程序在双击启动时默认以管理员身份运行.具体方法: 1 ...