Android 自定义View添加回调

      Android中回调的应用场景很多,自定义View中监听Edittext输入字符便是其中一种。闲话少叙,直接上代码:

1、自定义View

public class SearchLayout extends FrameLayout {
    private TextView search_close_btn;
    private EditText search_text;
    // 回调接口
    private ICallBack mCallBack;// 搜索按键回调接口

    public SearchLayout(Context context) {
        this(context,null);
    }

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

    public SearchLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.search_view_layout, this);
        search_close_btn = (TextView) view.findViewById(R.id.search_close_btn);
        search_close_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                search_text.setText("");
                KeybordUtil.closeKeybord(search_text,getContext());
                if(search_close_btn.getVisibility()==VISIBLE){
                    search_close_btn.setVisibility(GONE);
                }
            }
        });
        search_text = (EditText) view.findViewById(R.id.search_text);
        search_text.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        search_text.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        search_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {
                    ToastUtil.makeText(getContext(),"搜索:"+search_text.getText());
                    KeybordUtil.closeKeybord(search_text,getContext());
                    return true;
                }
                return false;
            }
        });

        search_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!TextUtils.isEmpty(search_text.getText())&&search_close_btn.getVisibility()==GONE){
                    search_close_btn.setVisibility(VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!(mCallBack == null)){
                    mCallBack.SearchAciton(search_text.getText().toString());
                }
                if(TextUtils.isEmpty(search_text.getText())){
                    search_close_btn.setVisibility(GONE);
                }
            }
        });
    }

    /**
     * 点击键盘中搜索键后的操作,用于接口回调
     */
    public void setOnClickSearch(ICallBack mCallBack){
        this.mCallBack = mCallBack;
    }

    public interface ICallBack {
        void SearchAciton(String string);
    }

}

2、布局中引入自定义View:

<com.humorboy.view.SearchLayout
    android:id="@+id/searchView"
    android:layout_below="@+id/custom_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

3、在Activity中监听:

searchView = (SearchLayout) findViewById(R.id.searchView);
searchView.setOnClickSearch(new SearchLayout.ICallBack() {
    @Override
    public void SearchAciton(String string) {
        ToastUtil.makeText(AutoComeActivity.this,""+string);
    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值