您的位置:首页 > 移动开发 > Android开发

Android学习笔记:自定义组合控件--打造可下拉的EditText--DropEditText

2017-11-08 20:55 459 查看
由于安卓并没有这样的控件,所以在需要用到的时候,只能够自己通过将EditText和Listview组合起来实现需求功能,为了方便使用,可以自定义一个组合控件,这样使用起来就很方便的啦!

实现过程:

首先一个布局文件,里面放一个editText和个人自定义的控件(也可以用Imageview或者其他view,主要是用来实现点击的)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<EditText  android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/bg_edit"
android:id="@+id/edittext"
android:layout_height="wrap_content"/>
<com.example.k555l.testtwo.DiyView.ClickView
android:id="@+id/textOnclick"
android:layout_width="10dp"
android:background="@drawable/bg_edit"
android:layout_height="match_parent" />
</LinearLayout>

自定义DropEditText

public class DropEditText extends FrameLayout implements View.OnClickListener{
EditText editText;
ClickView clickView;  //用来点击的view
View popupWindowView;  //popupWindow的布局
PopupWindow popupWindow;
WrapListView listView;   //popupWindow布局内的listview
String hitstring;  //edittext的hint文本
List <String>list; //listview数组

void init(Context context, AttributeSet attributeSet) {
View view = LayoutInflater.from(context).inflate(R.layout.dropedittext,this, true); //DropEditText的布局文件
editText = view.findViewById(R.id.edittext);
clickView = view.findViewById(R.id.textOnclick);
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.DropEditText);
hitstring = typedArray.getString(R.styleable.DropEditText_hitstring);
typedArray.recycle();

if (!TextUtils.isEmpty(hitstring))
editText.setHint(hitstring);

popupWindowView = LayoutInflater.from(context).inflate(R.layout.popwindownview, null);
listView = popupWindowView.findViewById(R.id.listview);
listView.setOnItemClickListener(new Mylister());
clickView.setOnClickListener(this);
editText.setOnKeyListener(new MyOnKeyListener());
}

public DropEditText(@NonNull Context context) {
super(context);
}

public DropEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}

public DropEditText(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
listView.setListWidth(getMeasuredWidth());//设置listview的宽
}

/***
* listview点击监听
*/
class Mylister implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (list!=null){
editText.setText(list.get(i));
popupWindow.dismiss();
}

}
}

/***
* 软键盘监听
* 确认后添加的listview
*/
class MyOnKeyListener implements  OnKeyListener{
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (i==KeyEvent.KEYCODE_ENTER&&keyEvent.getAction()==KeyEvent.ACTION_DOWN){
InputMethodManager inputMethodManager=(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()){
inputMethodManager.hideSoftInputFromWindow(getApplicationWindowToken(),0);
}
if (!TextUtils.isEmpty(editText.getText().toString().trim())&&list!=null){
list.add(editText.getText().toString().trim());
}
return true;
}
return false;
}
}

/***
* 为listview设置适配器
* @param adapter
* @param list
*/
public void SetAdater(BaseAdapter adapter,List<String>list) {
this.list=list;
listView.setAdapter(adapter);
popupWindow=new PopupWindow(popupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setFocusable(true);
}

/***
* 点击弹出listview列表
* @param view
*/
@Override
public void onClick(View view) {
if(view.getId()==R.id.textOnclick){
if (popupWindow.isShowing()){
popupWindow.dismiss();
return;
}
else{
popupWindow.showAsDropDown(this,0,0);
}

}
}
}


ListView默认是占满横向的屏幕的所以我们可以重写ListView的onMeasure方法改变它的测量机制。

public class WrapListView extends ListView {
private int mWidth = 0;

public WrapListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public WrapListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

// 重写onMeasure方法 解决默认横向占满屏幕问题
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int height = getMeasuredHeight();
// measureChildren(widthMeasureSpec, heightMeasureSpec);
for(int i=0;i<getChildCount();i++) {
int childWidth = getChildAt(i).getMeasuredWidth();
mWidth = Math.max(mWidth, childWidth);
}

setMeasuredDimension(mWidth, height);
}

/**
* 设置宽度,如果不设置,则默认包裹内容
* @param width 宽度
*/
protected void setListWidth(int width) {
mWidth = width;
}
}

调用了一下父类的onMeasure方法,为的就是利用ListView默认的测量机制获取总高度。在我们获取了测量后的高度后,接下来就是一个for循环,取出每一个item,并获取他的高度。mWidth的值就是子item中最宽的那个item的宽度(当然在下面我们提供的方法中可以手动修改mWidth的值)。

还有popwindow的布局文件和list view的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/listviewitem_text"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


attrs属性集

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DropEditText">
<attr name="hitstring" format="string"/>
</declare-styleable>
</resources>


在activity中这样使用

DropEditText dropEditText=view.findViewById(R.id.dropedittext);
list=new ArrayList<>();
list.add("选项一");
list.add("选项二");
list.add("选项三");
list.add("选项四");
dropEditText.SetAdater(new mydater(),list);


至此,DropEditText 功能基本实现,不足之处,还望留言指出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局