先上图:

   

XML为:

<LinearLayout 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:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:layout_gravity="center_horizontal"> <ImageButton
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@null"
android:layout_gravity="center_horizontal"
android:src="@drawable/btn_icon"
android:textColor="#0A84BD"
android:textSize="16sp" /> <EditText
android:id="@+id/custom_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/btn_margin_left_right"
android:layout_marginRight="@dimen/btn_margin_left_right"
android:background="@drawable/animation_edit_background"
android:gravity="left|center_vertical"
android:hint="username"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:maxLength="25"
android:textColor="#000000"
android:textColorHint="#AEAEAE"
android:textSize="16sp" /> <EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/btn_margin_left_right"
android:layout_marginRight="@dimen/btn_margin_left_right"
android:background="@drawable/animation_edit_background"
android:gravity="left|center_vertical"
android:hint="password"
android:inputType="phone|numberDecimal"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:maxLength="25"
android:textColor="#000000"
android:textColorHint="#AEAEAE"
android:textSize="16sp" /> <Button
android:id="@+id/btn_confirm"
android:layout_width="fill_parent"
android:gravity="center"
android:text="确定"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/button_item_selector"
android:textColor="#ffffff"
android:textSize="16sp" /> </LinearLayout>

重要的是EditText和Button的样式:

关于EditText:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#000000" />
<corners android:radius="@dimen/default_button_radius" />
<padding
android:bottom="@dimen/default_button_padding"
android:left="@dimen/default_button_padding"
android:right="@dimen/default_button_padding"
android:top="@dimen/default_button_padding" />
</shape>

Button:

<?xml version="1.0" encoding="utf-8"?

>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0A84BD" />
<corners android:radius="@dimen/default_button_radius" />
<padding
android:bottom="@dimen/default_button_padding"
android:left="@dimen/default_button_padding"
android:right="@dimen/default_button_padding"
android:top="@dimen/default_button_padding" />
</shape>

让EditText自带clear的功能,能够用在EditText的右側增加一个clear的小图标来实现:

我们用setCompoundDrawablesWithIntrinsicBounds来做,这个函数是在控件的Left,Top,Right,Button设置图标。非常实用:

	final int btn_margin_left_right = this.getResources().getDimensionPixelSize(R.dimen.btn_margin_left_right);
final EditText custom_edittext = (EditText) findViewById(R.id.custom_edittext);
custom_edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void afterTextChanged(Editable s) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() > 0) {
custom_edittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.search_bar_close_btn, 0);
} else {
custom_edittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
} }
});

为了捕捉用户点击clear图标的事件,要捕捉onTouch事件,在用户抬起手的时候推断假设手指的坐标在clear的图标那里,就清楚EditText的文字:

custom_edittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if(event.getAction() == MotionEvent.ACTION_UP) {
Drawable[] compoundDrawables = custom_edittext.getCompoundDrawables();
if(compoundDrawables != null) {
Drawable image = compoundDrawables[DRAWABLE_RIGHT]; if(image != null) {
int leftMargin = custom_edittext.getRight() - custom_edittext.getPaddingRight() - image.getBounds().width() - btn_margin_left_right;
if(event.getX() >= leftMargin) {
if (custom_edittext != null && custom_edittext.getEditableText() != null) {
custom_edittext.getEditableText().clear();
}
}
}
}
}
return false;
}
});

代码能够在http://blog.csdn.net/baidu_nod/article/details/37655327下载

假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)的更多相关文章

  1. 运用HTML5+CSS3和CSS滤镜做的精美的登录界面

    原始出处http://chenjinfei.blog.51cto.com/2965201/774865 <!DOCTYPE HTML> <html> <head> ...

  2. 用PreferenceActivity做一个标准的设置界面

    最后接触到一个任务,做一个工厂设置,在我看来工厂设置不需要多美观,但是一定要方便修改,添加功能,再就是使用方便,我就想到了用PreferenceActivity,android系统的settings就 ...

  3. 假设做一个循环滚动UIScrollView

    先上效果图: 首先初始化: - (void)viewDidLoad { //加入最后一张图 用于循环 int length = 4; NSMutableArray *tempArray = [NSMu ...

  4. 一个简单WPF登陆界面,包含记住密码,自动登录等功能,简洁美观

    简介:这是一个自己以前用WPF设计的登陆界面,属于一个实验性的界面窗体,如果用于产品还很有不足.但也是有一点学习价值.后台代码略有复杂,但基本上都有注释 分类,略有代码经验的一般都能看懂. 登陆界面外 ...

  5. 用 JS 做一个数独游戏(二)

    用 JS 做一个数独游戏(二) 在 上一篇博客 中,我们通过 Node 运行了我们的 JavaScript 代码,在控制台中打印出来生成好的数独终盘.为了让我们的数独游戏能有良好的体验,这篇博客将会为 ...

  6. 用bootstrap做一个背景可轮转的登录界面

    用bootstrap做一个背景可轮转的登录界面 一.总结 一句话总结:用css3的动画的 @keyframes 规则,制作轮转图. 1.用bootstrap做一个背景可轮转的登录界面? a.动画部分用 ...

  7. 使用react-native做一个简单的应用-04界面主框架

    欢迎界面搭建完毕,我们接下来需要做的就是搭建应用程序的主体框架啦.首先我们看一下首页的截图: 从图中看到,我将首页分为了三部分:用黑色矩形表示的头部,绿色表示的内容和红色表示的底部. 下面我们需要解决 ...

  8. Android UI组件----用相对布局RelativeLayout做一个登陆界面

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  9. 致敬学长!J20航模遥控器开源项目计划【开局篇】 | 先做一个开机界面 | MATLAB图像二值化 | Img2Lcd图片取模 | OLED显示图片

    我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ...

随机推荐

  1. 0050 MyBatis关联映射--一对多关系

    一对多关系更加常见,比如用户和订单,一个用户可以有多个订单 DROP TABLE IF EXISTS customer; /*用户表*/ CREATE TABLE customer( `pk` INT ...

  2. JS对象序列化为JSON对象的方法

    var $ = $ || {}; /** * 将JS对象序列化为JSON字符串 * @param {Mixed} o The variable to decode * @return {String} ...

  3. jQuery Colorbox使用教程

    jQuery Colorbox是一款弹出层,内容播放插件,效果极佳,最关键的是大小只有10KB,当然我主要是用来弹出图片啦,(之前介绍过jquery Fancybox插件,个人很喜欢).jQuery ...

  4. Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console、file等。Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别。

    Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console.file等.Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别. ...

  5. (转)java volatile关键字

    转自:http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html 参考:http://blog.csdn.net/imzoer/ar ...

  6. hdu 1164:Eddy's research I(水题,数学题,筛法)

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. <转载> C++笔试、面试笔记

    这些东西有点烦,有点无聊.如果要去C++面试就看看吧.几年前网上搜索的.刚才看到,就整理一下,里面有些被我改了,感觉之前说的不对或不完善. 1.求下面函数的返回值( 微软) int func(x)  ...

  8. 面试题思考:什么是基于注解的切面实现?(AOP是Aspect Oriented Program的首字母缩写)

    首先解释下AOP :在程序运行时,动态的将代码切入到类的指定方法.指定位置上的编程思想就是面向切面编程 一般而言,我们管切入到指定类指定方法的代码片段为切面,而切入的哪些类.哪些方法则叫切入点.有了A ...

  9. 文件夹进行MD5校验的实现算法

    每份相同数据(文件夹)都可以生成一份唯一的md5校验文件,我们可以通过直接校验整个数据文件夹的方法来确定数据是否有误. 1.针对整个文件夹生成md5校验文件方法: 以data文件夹为例,我们需要得到d ...

  10. 基础知识《三》java修饰符

    一.修饰符 private 成员随时都是“私有”的,任何人不得访问.但在实际应用中,经常想把某些东西深深地藏起来,但同时允许访问衍生类的成员. protected 关键字可帮助我们做到这一点.它的意思 ...