分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
一、效果图
二、特点
1.简单。只有100多行的类,无任何依赖,也没有自定义属性
2.没有用到组合控件的实现方法,少了很多布局文件等一系列东西
3.点击下拉图标时隐藏软键盘并弹出popWindow,点击item显示对应项的文字
4.下拉和上拉的自动切换
5.易用。可见调用示例
三、调用示例
1.activity_main.xml中声明
- <com.afei.myedittext.DropEditText
- android:id="@+id/drop_edit_text"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- package com.afei.myedittext;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- public class MainActivity extends AppCompatActivity {
- private DropEditText dropEditText;
- private ArrayAdapter<String> adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- dropEditText = (DropEditText) findViewById(R.id.drop_edit_text);
- String[] strings = new String[10];
- for (int i = 0; i < 10; i++) {
- strings[i] = "美女" + i + "号";
- }
- adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
- dropEditText.setAdapter(adapter);
- }
- }
四、DropEditText代码
- package com.afei.myedittext;
- import android.content.Context;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.PopupWindow;
- public class DropEditText extends EditText implements AdapterView.OnItemClickListener, PopupWindow.OnDismissListener {
- private Drawable mDrawable; // 显示的图
- private PopupWindow mPopupWindow; // 点击图片弹出的popWindow对象
- private ListView mPopListView; // popWindow的布局
- private int mDropDrawableResId; // 下拉图标
- private int mRiseDrawableResID; // 上拉图标
- public DropEditText(Context context) {
- this(context, null);
- }
- public DropEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
- public DropEditText(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(context);
- }
- private void init(Context context) {
- mPopListView = new ListView(context);
- mDropDrawableResId = R.mipmap.drop; // 设置下拉图标
- mRiseDrawableResID = R.mipmap.rise; // 设置上拉图标
- showDropDrawable(); // 默认显示下拉图标
- mPopListView.setOnItemClickListener(this);
- }
- /**
- * 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟点击事件
- * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
- */
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_UP) {
- if (getCompoundDrawables()[2] != null) {
- int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
- int end = getWidth(); // 结束位置
- boolean available = (event.getX() > start) && (event.getX() < end);
- if (available) {
- closeSoftInput();
- showPopWindow();
- return true;
- }
- }
- }
- return super.onTouchEvent(event);
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- super.onLayout(changed, left, top, right, bottom);
- if (changed) {
- mPopupWindow = new PopupWindow(mPopListView, getWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
- mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); // 设置popWindow背景颜色
- mPopupWindow.setFocusable(true); // 让popWindow获取焦点
- mPopupWindow.setOnDismissListener(this);
- }
- }
- private void showPopWindow() {
- mPopupWindow.showAsDropDown(this, 0, 5);
- showRiseDrawable();
- }
- private void showDropDrawable() {
- mDrawable = getResources().getDrawable(mDropDrawableResId);
- mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
- setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);
- }
- private void showRiseDrawable() {
- mDrawable = getResources().getDrawable(mRiseDrawableResID);
- mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
- setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);
- }
- public void setAdapter(BaseAdapter adapter) {
- mPopListView.setAdapter(adapter);
- }
- private void closeSoftInput() {
- InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- this.setText(mPopListView.getAdapter().getItem(position).toString()); // 可能需要你重写item的toString方法
- mPopupWindow.dismiss();
- }
- @Override
- public void onDismiss() {
- showDropDrawable(); // 当popWindow消失时显示下拉图标
- }
- }
五、注意事项
1.上拉和下拉的图片我已经指定死了,使用时替换成你需要的图片就行,你也可以写在自定义的attr中去
2.为了使用无脑点(但是这样就不够灵活了),不需要再去配置各种参数,很多东西我都写死了,没有预留一些方法给外面设置了(懒而已_(:з」∠)_),但是代码和注释都还是比较清楚的
3.这个DropEditText的理论和上一篇的ClearEditText其实是差不多的,原理都很简单,也可以拿来练习和研究自定义的View使用(自己改一下之类的)