自定义带提示的文本输入框

Android自带AutoCompleteTextView,功能是在输入的是否完成提示功能,需要根据输入内容进行提示,空内容没有提示。

在项目中不满足需求,要求需要类似于可输入内容的下拉框。根据AutoCompleteTextView源码进行自定义实现功能。

代码: 

/**
 * 带自动提示功能的EditText
 */

public class MyEditText extends EditText{
    public ListPopupWindow mPopupWindow;//提示框
    private ArrayAdapter arrayAdapter;//适配器
    public MyEditText(Context context) {
        super(context);
        initView(context);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    private void initView(final Context context){
        mPopupWindow=new ListPopupWindow(context);
        mPopupWindow.setAnchorView(this); //附着的View
        mPopupWindow.setVerticalOffset((int) getResources().getDimension(R.dimen.DIMEN_30PX)); //相对于父控件的垂直位置
        mPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                if(arrayAdapter!=null) {
                    MyEditText.this.setText(arrayAdapter.getItem(position));
                }
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                clearFocusable(MyEditText.this);
            }
        });
        this.addTextChangedListener(new TextWatcher() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    if (start + after > MyEditText.this.getMaxEms()) {
                        ToastUtil.showToast(context, "最多可输入"+MyEditText.this.getMaxEms()+"个字符");
                    }
                }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s)) {
                    mPopupWindow.show();
                }else{
                    mPopupWindow.dismiss();
                    getFocusable(MyEditText.this);
                }
            }
        });
        this.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText view = (EditText) v;
                if (hasFocus&&TextUtils.isEmpty(view.getText())) {
                    mPopupWindow.show();
                }
            }
        });
    }
    //设置适配器
    public void setArrayAdapter(ArrayAdapter arrayAdapter){
        this.arrayAdapter=arrayAdapter;
        mPopupWindow.setAdapter(arrayAdapter);
    }
    //清除控件焦点
    private void clearFocusable(View view){
        view.setFocusableInTouchMode(false);
        view.clearFocus();
        view.setFocusableInTouchMode(true);
    }
    //设置控件获取焦点
    private void getFocusable(EditText view){
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setSelection(view.getText().length());
    }
}

你可能感兴趣的:(android)