Android控件中的TextView控件仅仅有一个输入框。可是为了用于的操作方便我们应该实现一些功能:

1. 能够直接将内容删除的功能button

2. 可以记录用户曾经输入的数据,同一时候可以将数据通过下拉显示,点击的时候实现输入

先上图:

下拉的图片没有做。所以和删除的图片使用同一个了,同志们能够直接在xml文件里更换即可了

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

分析:

肯定要使用自己定义view来实现的。我们知道自己定义view大概能够分为三类:自绘控件,组合控件,继承控件,我们这里是要进行增强的textView的功能。所以我这里使用的

是组合控件的方式来进行实现

既然使用组合控件,那么我们就来看看究竟要使用什么控件来组合呢:

1.  当中一个必须是textView了

2. 下拉的那两个button是什么。当然是imageView了

3. 另一个下拉列表,。

。。。那就使用popwindow了

思路:

1. 怎样实现直接删除用户的输入

使用addTextChangedListener监听textView内容的变化的时间依据内容的变化进行确定是否显示删除button,同一时候绑定删除button的点击事件

2.怎样实现下拉显示用户输入过的数据,以及选中的时候实现输入

我们通过下拉button的点击进行确定popwindow窗体的显示,在popwindow的界面有一个listview,在这个listview的adpater中进行绑定条目的点击事件

那么我们在adapter中绑定的事件,怎样控制整个控件的功能输入呢,这里就是用handler了,在创建adapter的时候将handler传递过去,

然后当点击事件发生的时候我们使用handler进行send消息即可了,当然我们在send消息的时候将当前点击的数据传递过来即可了

上代码:

1. 控件主体代码

/**
* 自己定义的控件,自带删除的button,下拉button
* @author zcs
* */
public class EditTextClearSelect extends FrameLayout { private EditText editText; //显示的editText输入框
private ImageButton clearImageButton; //显示的用于进行删除editText中内容的button
private ImageButton selectImageButton; //显示的用于下拉editText中内容的button //PopupWindow对象 ,用于已下拉形式显示数据
private PopupWindow selectPopupWindow= null;
//自己定义Adapter
private ECS_OptionsAdapter optionsAdapter = null;
//下拉框选项数据源
private ArrayList<String> datas = new ArrayList<String>();
//下拉框依附组件
private LinearLayout parent;
//展示全部下拉选项的ListView
private ListView listView = null;
//用来处理选中或者删除下拉项消息
private Handler handler; public EditTextClearSelect(Context context) {
super(context);
}
//用于对自己定义的控件进行初始化
public EditTextClearSelect(Context context, AttributeSet attrs){
super(context, attrs);
//调用初始化自己定义控件的方法
init(context,attrs);
} /**
* 初始化下拉功能使用的组件
*/
private void initWedget(Context context){
//初始化Handler,用来处理消息
handler = new Handler(){
public void handleMessage(Message msg) {
//当adapter中传递过来消息以后依据选中的id,将相应的值填写到EditText组件中
Bundle data = msg.getData();
//选中下拉项,下拉框消失
int selIndex = data.getInt("selIndex");
editText.setText(datas.get(selIndex));
dismiss();
}
}; //假设没有数据。则下拉菜单不显示
if( !(datas.size() > 0) ){
selectImageButton.setVisibility(View.GONE);
} //设置点击下拉箭头图片事件,点击弹出PopupWindow浮动下拉框
selectImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取下拉框依附的组件宽度,然后又一次设置popWindow的宽度
selectPopupWindow.setWidth(parent.getWidth());
//显示PopupWindow窗体
popupWindwShowing();
}
}); //初始化PopupWindow
initPopuWindow(context);
} /**
* 初始化PopupWindow
*/
private void initPopuWindow(Context context){ //PopupWindow浮动下拉框布局
View loginwindow = LayoutInflater.from(context).inflate(R.layout.wecs_options, null);
listView = (ListView) loginwindow.findViewById(R.id.list); //设置自己定义Adapter
optionsAdapter = new ECS_OptionsAdapter(context,handler,datas);
listView.setAdapter(optionsAdapter); selectPopupWindow = new PopupWindow(loginwindow, 0,LayoutParams.WRAP_CONTENT, true);
selectPopupWindow.setOutsideTouchable(true);
//实现当点击屏幕其它地方的时候将当前的pop关闭
selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());
} /**
* 显示PopupWindow窗体
* @param popupwindow
*/
public void popupWindwShowing() {
//将pop窗体在自己定义控件的底部显示
selectPopupWindow.showAsDropDown(parent);
} /**
* PopupWindow消失
*/
public void dismiss(){
selectPopupWindow.dismiss();
} /**
* 初始化,包含添加删除button。下拉button
*/
public void init(Context context,AttributeSet attrs){
//获取自己定义控件的界面,相当于当前的自己定义View就使用的View
View view = LayoutInflater.from(context).inflate(R.layout.weight_edit_clear_select, this, true); parent = (LinearLayout) view.findViewById(R.id.parent); //当前的自己定义控件
editText = (EditText) view.findViewById(R.id.et); //输入框
clearImageButton = (ImageButton) view.findViewById(R.id.clear_ib); //删除button
selectImageButton = (ImageButton) view.findViewById(R.id.select_id); //下拉button //当点击删除button的会后将输入框数据清空
clearImageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
//依据输入框中的内容。决定是否显示删除button
editText.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
//输入框有内容,显示button
editText.setSelection(s.length());
clearImageButton.setVisibility(View.VISIBLE);
} else {
clearImageButton.setVisibility(View.GONE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
} }); //初始化pop组件,设置下拉button的功能
initWedget(context); //将属性值设置到控件中
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextClearSelect);
//输入框的默认的显示文本
CharSequence text = a.getText(R.styleable.EditTextClearSelect_textECS);
CharSequence hint = a.getText(R.styleable.EditTextClearSelect_hintECS);
CharSequence parent_width = a.getText(R.styleable.EditTextClearSelect_layout_width); if (text!=null&&!"".equals(text.toString().trim())) {
editText.setText(text);
//设置光标位置
editText.setSelection(text.length());
this.clearImageButton.setVisibility(View.VISIBLE);
}
if (hint!=null&&!"".equals(hint.toString().trim())) {
editText.setHint(hint);
}
if(parent_width!=null && !"".equals(parent_width.toString().trim()) ){
//设置当前控件的宽度,为屏幕的百度比有參数进行设置
LayoutParams parent_lp = (LayoutParams) parent.getLayoutParams();
parent_lp.width = (int) (AppUtil.getScreenDispaly(context)[0] * ( (Double)(Double.parseDouble(parent_width.toString()) / 100) ));
Log.i("控件宽度", parent_lp.width+"");
parent.setLayoutParams(parent_lp);
}
a.recycle();
} /**
* 获得输入的值
* @return
*/
public String getText(){
return this.editText.getText().toString();
} /**
* 设置值
* @param text
*/
public void setText(String text){
this.editText.setText(text);
} /**
* 设置默认值
* @param hint
*/
public void setHint(String hint){
this.editText.setHint(hint);
} /**
* 获得输入框控件
* @return
*/
public EditText getEditText(){
return this.editText;
} /**
* 获得消除button
* @return
*/
public ImageButton getClearImageButton(){
return this.clearImageButton;
} //设置下拉列表中的选项值
public void setOptionsValue(ArrayList<String> inDatas){
datas.clear();
if( (inDatas ==null) || !(inDatas.size() > 0) ){
selectImageButton.setVisibility(View.GONE);
}else{
selectImageButton.setVisibility(View.VISIBLE);
datas.addAll(inDatas);
}
optionsAdapter.notifyDataSetChanged();
}

2. popwindow里面listview的适配器

public class ECS_OptionsAdapter extends BaseAdapter {

	  	private ArrayList<String> list = new ArrayList<String>();
private Context context = null;
//传递过来的hanler,用于进行通知操作(这里是通知自己定义的view要继续改动editText中的数据)
private Handler handler; public ECS_OptionsAdapter(Context context,Handler handler,ArrayList<String> list){
this.context = context;
this.handler = handler;
this.list = list;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
//下拉项布局
convertView = LayoutInflater.from(context).inflate(R.layout.wecs_option_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.item_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(list.get(position));
//为下拉框选项文字部分设置事件。终于效果是点击将其文字填充到文本框
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//当点击的时候进行发送消息。通知组件进行改动数据
Message msg = new Message();
//设置要传递的数据
Bundle data = new Bundle();
//设置选中索引
data.putInt("selIndex", position);
msg.setData(data);
//发出消息
handler.sendMessage(msg);
}
});
return convertView;
} } class ViewHolder {
TextView textView;
}

3. 使用

	private EditTextClearSelect etcs;

	@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} //初始化
private void init(){
String selectDatas = "admin,login,user";
etcs = (EditTextClearSelect) findViewById(R.id.username_edit);
//为控件设置下拉的数据
etcs.setOptionsValue( new ArrayList<String>(Arrays.asList(selectDatas.split(","))) ); }

ok搞定

源代码下载

android自己定义TextView的更多相关文章

  1. android 自己定义TextView&quot;会发脾气的TextView&quot;

    转载请注明出处王亟亟的大牛路 Git上看到的一个自己定义控件就搞来研究研究.蛮可爱的. 项目结构: 执行效果:非常Q谈.谈的图片什么都 都能够换哦 自己定义View: public class Jel ...

  2. Android 自己定义TextView 实现文本间距

    转载请标明出处: http://blog.csdn.net/u011974987/article/details/50845269: Android系统中TextView默认显示中文时会比較紧凑.不是 ...

  3. Android 自己定义 TextView drawableTop 图标与文字左对齐(效果图)

    public class DrawableTopLeftTextView extends TextView { private Paint mPaint; private float fFontHei ...

  4. Android之——自己定义TextView

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47082241 在这一篇博文中,将向大家介绍怎样以最简单的方式,来自己定义Andro ...

  5. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

  6. android Notification定义与应用

    首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...

  7. Android学习之-TextView的滑动效果

    textView中如何设置滚动条 在xml中定义: <TextView            android:layout_width="wrap_content"      ...

  8. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  9. android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)

    自定义一个dialog: 之前有很多博客都有过这方面的介绍,可是个人觉得通常不是很全面,通用性不是很强,一般会定义一个自己的dialog类,然后去使用,难道每一个dialog都要定义一个class吗? ...

随机推荐

  1. AC日记——[SCOI2007]蜥蜴 bzoj 1066

    1066 思路: 网络流最大流: 拆点,每个点拆成两个,流量为这个点的高度: 注意,文中说的距离是曼哈顿距离(劳资以为开根号wa了不知道多少次): 每两个距离不大于d的点连边,流量inf: 如果距离能 ...

  2. Nginx修改版本信息或隐藏版本号

    一,隐藏版本号.首先说明,这个是某一方面隐藏,不是彻底隐藏.未隐藏之前查看nginx信息: 隐藏方法: 修改nginx.conf配置文件,在http { } 标签里边加入字段: server_toke ...

  3. (6)python tkinter-容器、子窗体

    Frame f = tkinter.Frame(width=380, height=270, bg='white').pack() LabelFrame f = tkinter.LabelFrame( ...

  4. P1450 包裹快递 RP+14【二分】

    [题目链接]:https://vijos.org/p/category/%E5%85%B6%E4%BB%96,%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE 描述 一个快递公 ...

  5. luogu P3800 Power收集

    题目背景 据说在红雾异变时,博丽灵梦单身前往红魔馆,用十分强硬的手段将事件解决了. 然而当时灵梦在Power达到MAX之前,不具有“上线收点”的能力,所以她想要知道她能收集多少P点,然而这个问题她答不 ...

  6. Enter Query Mode Search Tricks Using Enter_Query Built-in in Oracle Forms

    In this post you will learn how to specify any condition in enter query mode of Oracle Forms. Whenev ...

  7. 第一次用THINKPHP 报路径错

    我第一次 看网上写的代码 define('THINK_PATH','ThinkPHP');define('App_NAME','43');define('App_PATH','.'); require ...

  8. Error Code: 1055 incompatible with sql_mode=only_full_group_by

    OperationalError at / (1055, "Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...

  9. 排序算法之高速排序(Java)

    //高速排序 public class Quick_Sort { // 排序的主要算法 private int Partition(int[] data, int start, int end) { ...

  10. Apache OFBIZ高速上手(三)--文件夹&amp;&amp;配置文件介绍

    1.OFBiz简单介绍,什么是OFBiz           OFBiz is an Apache Software Foundation top level project.           A ...