首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

Android 带扫除功能的输入框控件ClearEditText,仿IOS的输入框

2013-11-08 
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框转载请注明出处http://blog.csdn.net/xiaanmi

Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685


今天给大家带来一个很实用的小控件ClearEditText,就是在Android系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容,IOS上面直接设置某个属性就可以实现这一功能,但是Android原生EditText不具备此功能,所以要想实现这一功能我们需要重写EditText,接下来就带大家来实现这一小小的功能

我们知道,我们可以为我们的输入框在上下左右设置图片,所以我们可以利用属性android:drawableRight设置我们的删除小图标,如图

Android 带扫除功能的输入框控件ClearEditText,仿IOS的输入框

我这里设置了左边和右边的图片,如果我们能为右边的图片设置监听,点击右边的图片清除输入框的内容并隐藏删除图标,这样子这个小功能就迎刃而解了,可是Android并没有给允许我们给右边小图标加监听的功能,这时候你是不是发现这条路走不通呢,其实不是,我们可能模拟点击事件,用输入框的的onTouchEvent()方法来模拟,

当我们触摸抬起(就是ACTION_UP的时候)的范围  大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,当然我这里没有考虑竖直方向,只要给清除小图标就上了监听,其他的就都好处理了,我先把代码贴上来,在讲解下

<RelativeLayout 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="#95CAE4">    <com.example.clearedittext.ClearEditText        android:id="@+id/username"        android:layout_marginTop="60dp"        android:layout_width="fill_parent"        android:background="@drawable/login_edittext_bg"         android:drawableLeft="@drawable/icon_user"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:singleLine="true"        android:drawableRight="@drawable/delete_selector"        android:hint="输入用户名"        android:layout_height="wrap_content" >    </com.example.clearedittext.ClearEditText>    <com.example.clearedittext.ClearEditText        android:id="@+id/password"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:layout_marginTop="10dip"        android:drawableLeft="@drawable/account_icon"        android:hint="输入密码"        android:singleLine="true"        android:password="true"        android:drawableRight="@drawable/delete_selector"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/username"        android:background="@drawable/login_edittext_bg" >    </com.example.clearedittext.ClearEditText>    <Button        android:id="@+id/login"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:background="@drawable/login_button_bg"        android:textSize="18sp"        android:textColor="@android:color/white"        android:layout_below="@+id/password"        android:layout_marginTop="25dp"        android:text="登录" /></RelativeLayout>
然后就是界面代码的编写,主要是测试输入框左右晃动而已,比较简单

package com.example.clearedittext;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Toast mToast;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ClearEditText username = (ClearEditText) findViewById(R.id.username);final ClearEditText password = (ClearEditText) findViewById(R.id.password);((Button) findViewById(R.id.login)).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(TextUtils.isEmpty(username.getText())){//设置晃动username.setShakeAnimation();//设置提示showToast("用户名不能为空");return;}if(TextUtils.isEmpty(password.getText())){password.setShakeAnimation();showToast("密码不能为空");return;}}});}/** * 显示Toast消息 * @param msg */private void showToast(String msg){if(mToast == null){mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);}else{mToast.setText(msg);}mToast.show();}}

运行项目,如图,悲剧,没有动画效果,算了就这样子吧,你是不是也想把它加到你的项目当中去呢,赶紧吧!

Android 带扫除功能的输入框控件ClearEditText,仿IOS的输入框




项目源码,点击下载


1楼u011423845昨天 09:50
项目用上了,谢谢博主

热点排行