如果做一个精美的Login界面(解决了一EditText自带clear的功能,类似iphone的UITextField)
来源:互联网 发布:东莞淘宝美工培训学费 编辑:程序博客网 时间:2024/05/18 00:16
先上图:
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() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void afterTextChanged(Editable s) {}@Overridepublic 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下载
0 0
- 如果做一个精美的Login界面(解决了一EditText自带clear的功能,类似iphone的UITextField)
- 如果windows自带的刻录功能没了怎么办
- ClearEditText,自带清除功能的EditText
- EditText自带删除的功能
- ClearEditText,自带清除功能的EditText
- 自带删除功能的EditText
- Android 用EditText加一个ImageButton自定义一个带clear的搜索框
- iphone 上实现类似iphone自带股票软件下端的页面循环滚动
- 精美的聊天界面
- 带删除功能的EditText
- 带删除功能的edittext
- 带验证功能的 editText
- 带清除功能的EditText
- 带删除功能的EditText
- 做了一个带标记的textField
- 如何自定义一个带删除功能的EditText
- mobile下,iphone类似界面的开发
- Idea自带的文件过滤功能,功能类似gitignore不过只能过滤文件夹
- Linux 下修改Oracle 的字符集:WE8ISO8859P1 修改为 ZHS16GBK
- [c语言]模拟银行atm机,实现存款、取款等业务
- Scolloview中嵌套listview,则焦点冲突,listview只显示一行
- Eclipse开发andriod程序时import android.support.v4或者import android.support.v7提示导入错误解决办法
- 三种方法解决android帮助文档打开慢
- 如果做一个精美的Login界面(解决了一EditText自带clear的功能,类似iphone的UITextField)
- maven的安装
- 基于plusgantt的项目管理系统实战开发视频教程
- Logic java 第一章初识java
- 局部刷新UITableView
- Swift中的标准函数
- 黑马程序员—反射的深入讲解
- 函数和堆栈
- C++继承中的内存布局