实现方案:
ic_search_s2.png
ic_delete.png
ic_delete_p.png
1、selector_delete_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_delete_p" android:state_pressed="true" />
<item android:drawable="@mipmap/ic_delete" />
</selector>
2、shape_corner_rect_3.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3dp"/>
<solid android:color="@color/chart_bg_color"/>
<stroke android:width="1dp" android:color="@color/chart_bg_color"/>
</shape>
3、组合控件布局lay_searchbox.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height2"
android:background="@drawable/shape_corner_rect_3">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_search_s2"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="38dp"
android:layout_centerVertical="true"
android:hint="@string/t_dcfrag_searchtip"
android:textColorHint="@color/edit_text_color"
android:singleLine="true"
android:imeOptions="actionSearch"
android:background="@null"
android:textColor="@color/white"
android:textSize="@dimen/textsize_h2"/>
<ImageView
android:id="@+id/delete"
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/selector_del_click"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:visibility="gone"/>
</RelativeLayout>
注意:
图片标红的地方必须配置,否则软键盘回车键不会变成搜索按钮。
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、控件调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:background="@drawable/shape_gradient_rect"
android:orientation="vertical"
android:paddingLeft="@dimen/action_bar_margin"
android:paddingRight="@dimen/action_bar_margin">
<dsdf.com.view.SearchBox
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
完!!!
注意:软键盘把布局顶上去时,可以在项目清单文件AndroidManifest.xml中对应的Activity配置
<activity
android:name=".HomeActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden"/>
android:windowSoftInputMode="adjustPan|stateHidden"
即可解决。