先上图:

   

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. CCNA2.0笔记_ipv6的EIGRP

    IPv6的eigrp特征: 邻居发现 增量更新 快速收敛 负载均衡 三个表 -邻居表 -拓扑表 -路由表 配置ipv6的eigrp Router(config)#ipv6 unicast-routin ...

  2. C# 时间格式 yyyy/mm/dd

    今天遇到个问题在C#中将日期格式设置为yyyy/MM/dd,我是这样写的: DateTime.Now.ToString("yyyy/MM/dd"); 可是获取到的日期还是显示yyy ...

  3. BT下载原理分析

    版权声明:本文为博主原创文章,未经博主允许不得转载. BitTorrent协议. BT全名为BitTorrent,是一个p2p软件,你在下载download的同时,也在为其他用户提供上传upload, ...

  4. 13 jsp include

    假如您有一系列的页面, 每一个都拥有同样的导航栏, 联系信息和注脚, 好的解决方案是使用 jsp:include, 它可以将下面列出的任何内容插入到jsp的输出中: html 页面内容 纯文本文档的内 ...

  5. $.messager.show扩展:指定位置显示

    扩展了个$.messager.showBySite,根据舍得的位置显示$.messager.show.代码如下: /** * 指定位置显示$.messager.show * options $.mes ...

  6. 第一百六十节,封装库--JavaScript,ajax注册表单到数据库

    封装库--JavaScript,ajax注册表单到数据库 效果图 前台js var biaodan = $().xu_lie_biao_dan($('form').sh_jd()); //序列化获取表 ...

  7. ubuntu 安装 avahi服务

    sudo apt-get install avahi-daemon sudo apt-get install avahi-utils

  8. convolutional neural network 课程笔记

    一.CNN基础 (1)CNN在CV方面的应用 image classification(图像识别).object detection(目标检测).neural style transfer(风格迁移) ...

  9. retrival and clustering : week 3 k-means 笔记

    华盛顿大学 machine learning 笔记. K-means algorithm 算法步骤: 0. 初始化几个聚类中心 (cluster centers)μ1,μ2, … , μk 1. 将所 ...

  10. 转载 web前端简历

    Web前端程序员简历模板 本简历模板由国内首家互联网人才拍卖网站「 JobDeer.com 」提供. (括号里的是我们的顾问编写的说明,建议在简历书写完成后统一删除) 先讲讲怎样才是一份好的技术简历 ...