DropEditText,带下拉功能的EditText

来源:互联网 发布:淘宝拍卖车是真的吗 编辑:程序博客网 时间:2024/05/16 12:43

DropEditText,带下拉功能的EditText

标签: androidDropEditText自定义view
526人阅读 评论(0)收藏举报
分类:

目录(?)[+]

一、效果图



二、特点

1.简单。只有100多行的类,无任何依赖,也没有自定义属性

2.没有用到组合控件的实现方法,少了很多布局文件等一系列东西

3.点击下拉图标时隐藏软键盘并弹出popWindow,点击item显示对应项的文字

4.下拉和上拉的自动切换

5.易用。可见调用示例


三、调用示例

1.activity_main.xml中声明

[html] view plain copy
  1. <com.afei.myedittext.DropEditText  
  2.     android:id="@+id/drop_edit_text"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" />  
2.MainActivity.class中使用

[java] view plain copy
  1. package com.afei.myedittext;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.ArrayAdapter;  
  6.   
  7. public class MainActivity extends AppCompatActivity {  
  8.   
  9.     private DropEditText dropEditText;  
  10.     private ArrayAdapter<String> adapter;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         init();  
  17.     }  
  18.   
  19.     private void init() {  
  20.         dropEditText = (DropEditText) findViewById(R.id.drop_edit_text);  
  21.         String[] strings = new String[10];  
  22.         for (int i = 0; i < 10; i++) {  
  23.             strings[i] = "美女" + i + "号";  
  24.         }  
  25.         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);  
  26.         dropEditText.setAdapter(adapter);  
  27.     }  
  28. }  

四、DropEditText代码

[java] view plain copy
  1. package com.afei.myedittext;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Color;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.util.AttributeSet;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.view.inputmethod.InputMethodManager;  
  11. import android.widget.AdapterView;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.EditText;  
  14. import android.widget.LinearLayout;  
  15. import android.widget.ListView;  
  16. import android.widget.PopupWindow;  
  17.   
  18. public class DropEditText extends EditText implements AdapterView.OnItemClickListener, PopupWindow.OnDismissListener {  
  19.   
  20.     private Drawable mDrawable; // 显示的图  
  21.     private PopupWindow mPopupWindow; // 点击图片弹出的popWindow对象  
  22.     private ListView mPopListView; // popWindow的布局  
  23.     private int mDropDrawableResId; // 下拉图标  
  24.     private int mRiseDrawableResID; // 上拉图标  
  25.   
  26.     public DropEditText(Context context) {  
  27.         this(context, null);  
  28.     }  
  29.   
  30.     public DropEditText(Context context, AttributeSet attrs) {  
  31.         super(context, attrs);  
  32.         init(context);  
  33.     }  
  34.   
  35.     public DropEditText(Context context, AttributeSet attrs, int defStyle) {  
  36.         super(context, attrs, defStyle);  
  37.         init(context);  
  38.     }  
  39.   
  40.     private void init(Context context) {  
  41.         mPopListView = new ListView(context);  
  42.         mDropDrawableResId = R.mipmap.drop; // 设置下拉图标  
  43.         mRiseDrawableResID = R.mipmap.rise; // 设置上拉图标  
  44.         showDropDrawable(); // 默认显示下拉图标  
  45.         mPopListView.setOnItemClickListener(this);  
  46.     }  
  47.   
  48.     /** 
  49.      * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟点击事件 
  50.      * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效 
  51.      */  
  52.     @Override  
  53.     public boolean onTouchEvent(MotionEvent event) {  
  54.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  55.             if (getCompoundDrawables()[2] != null) {  
  56.                 int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置  
  57.                 int end = getWidth(); // 结束位置  
  58.                 boolean available = (event.getX() > start) && (event.getX() < end);  
  59.                 if (available) {  
  60.                     closeSoftInput();  
  61.                     showPopWindow();  
  62.                     return true;  
  63.                 }  
  64.             }  
  65.         }  
  66.         return super.onTouchEvent(event);  
  67.     }  
  68.   
  69.     @Override  
  70.     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  
  71.         super.onLayout(changed, left, top, right, bottom);  
  72.         if (changed) {  
  73.             mPopupWindow = new PopupWindow(mPopListView, getWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);  
  74.             mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); // 设置popWindow背景颜色  
  75.             mPopupWindow.setFocusable(true); // 让popWindow获取焦点  
  76.             mPopupWindow.setOnDismissListener(this);  
  77.         }  
  78.     }  
  79.   
  80.     private void showPopWindow() {  
  81.         mPopupWindow.showAsDropDown(this05);  
  82.         showRiseDrawable();  
  83.     }  
  84.   
  85.     private void showDropDrawable() {  
  86.         mDrawable = getResources().getDrawable(mDropDrawableResId);  
  87.         mDrawable.setBounds(00, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());  
  88.         setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);  
  89.     }  
  90.   
  91.     private void showRiseDrawable() {  
  92.         mDrawable = getResources().getDrawable(mRiseDrawableResID);  
  93.         mDrawable.setBounds(00, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());  
  94.         setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);  
  95.     }  
  96.   
  97.     public void setAdapter(BaseAdapter adapter) {  
  98.         mPopListView.setAdapter(adapter);  
  99.     }  
  100.   
  101.     private void closeSoftInput() {  
  102.         InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
  103.         imm.hideSoftInputFromWindow(this.getWindowToken(), 0);  
  104.     }  
  105.   
  106.     @Override  
  107.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  108.         this.setText(mPopListView.getAdapter().getItem(position).toString()); // 可能需要你重写item的toString方法  
  109.         mPopupWindow.dismiss();  
  110.     }  
  111.   
  112.     @Override  
  113.     public void onDismiss() {  
  114.         showDropDrawable(); // 当popWindow消失时显示下拉图标  
  115.     }  
  116. }  


五、注意事项

1.上拉和下拉的图片我已经指定死了,使用时替换成你需要的图片就行,你也可以写在自定义的attr中去

2.为了使用无脑点(但是这样就不够灵活了),不需要再去配置各种参数,很多东西我都写死了,没有预留一些方法给外面设置了(懒而已_(:з」∠)_),但是代码和注释都还是比较清楚的

3.这个DropEditText的理论和上一篇的ClearEditText其实是差不多的,原理都很简单,也可以拿来练习和研究自定义的View使用(自己改一下之类的)

0 0
原创粉丝点击