Android自定义SearchBox

Android自定义SearchBox_第1张图片

实现方案:

ic_search_s2.png

ic_delete.png

ic_delete_p.png

1、selector_delete_click.xml




    

    

2、shape_corner_rect_3.xml



    
    
    

3、组合控件布局lay_searchbox.xml




    

    

    


注意:

Android自定义SearchBox_第2张图片

图片标红的地方必须配置,否则软键盘回车键不会变成搜索按钮。

4、组合控件实现SearchBox.java

package dsdf.com.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import dsdf.com.R;

/**
 * 搜索框
 * Created by xl on 2019/1/11.
 */
public class SearchBox extends RelativeLayout implements View.OnKeyListener, TextWatcher, View.OnFocusChangeListener, View.OnClickListener {

    private Context m_context;
    private EditText m_edittext;
    private ImageView m_imgDel;
    //软键盘搜索键监听
    private OnSearchClickListener m_listener;
    //文本框文字变化监听
    private TextWatcher m_textWatcher;
    //文本框焦点监听
    private OnFocusChangeListener m_focusListener;
    private String m_strKeyWords = "";
    //点击搜索按钮响应两次
    private int m_iCount = 0;

    public SearchBox(Context context) {
        super(context);
        m_context = context;
        init();
    }

    public SearchBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        m_context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(m_context).inflate(R.layout.lay_searchbox, this);
        m_imgDel = (ImageView) findViewById(R.id.delete);
        m_edittext = (EditText) findViewById(R.id.edittext);
        //设置按键监听
        m_edittext.setOnKeyListener(this);
        //文本框监听
        m_edittext.addTextChangedListener(this);
        m_edittext.setOnFocusChangeListener(this);
        //设置del点击监听
        m_imgDel.setOnClickListener(this);
    }

    /**
     * 软键盘监听
     *
     * @param view
     * @param keyCode
     * @param keyEvent
     * @return
     */
    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if ((keyCode == KeyEvent.KEYCODE_ENTER) && m_listener != null) {
            //隐藏软键盘
            InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
            }
            if(m_iCount++ % 2 == 0){
                m_listener.onSearchClick(view);
            }
        }
        return false;
    }

    /**
     * 文本变化监听
     */
    public void setOnTextChangeListener(TextWatcher watcher) {
        this.m_textWatcher = watcher;
    }

    /**
     * 设置焦点变化监听
     */
    public void setOnFocuChangeListener(OnFocusChangeListener listener) {
        this.m_focusListener = listener;
    }

    /**
     * 设置接口
     *
     * @param listener
     */
    public void setOnSearchClickListener(OnSearchClickListener listener) {
        this.m_listener = listener;
    }

    /**
     * 定义接口
     */
    public interface OnSearchClickListener {
        void onSearchClick(View view);
    }

    /**
     * 按钮监听
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.delete) {
            m_edittext.setText("");
        }
    }

    /**
     * 焦点监听
     *
     * @param view
     * @param b
     */
    @Override
    public void onFocusChange(View view, boolean b) {
        if (m_focusListener != null) {
            m_focusListener.onFocusChange(m_edittext, b);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (m_textWatcher != null) {
            m_textWatcher.beforeTextChanged(charSequence, i, i1, i2);
        }
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (m_textWatcher != null) {
            m_textWatcher.onTextChanged(charSequence, i, i1, i2);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
        m_strKeyWords = editable.toString();
        if (m_strKeyWords.equals("")) {
            m_imgDel.setVisibility(View.GONE);
        } else {
            m_imgDel.setVisibility(View.VISIBLE);
        }
        if (m_textWatcher != null) {
            m_textWatcher.afterTextChanged(editable);
        }
    }

}

5、控件调用




    

完!!!

注意:软键盘把布局顶上去时,可以在项目清单文件AndroidManifest.xml中对应的Activity配置

android:windowSoftInputMode="adjustPan|stateHidden"

即可解决。

你可能感兴趣的:(android)