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68sxr1WZw4mo9cA8Nqknr8edgmeMD9y12iyiHgLF48O7HWq8bnRawBvpUn9fGPr2aj3HrH0ulyh1wAwguX9Q/ojqZTKXF7H7x8y4vJaW3t94c5Zeg0AI+3+/flyvaWqpddz144kvT59MB69BoCM3bz/9bCH/Pr/SNJro8ki4h29/Am9BoBcdvr5MrluQl7/FUN7fWrpmNHrA/HoNQAMMP7nN/p1f8QzfOmvb/Q6abKIeOfulifY64mg1wAcl/Pz0f26H2wF41XU0uvyykl6DQDjUkoF7cB8S9VUqTfHL3WXpdfHr36W9PrEgXj0GgDyUNsqDMPuJx0GW0H0Ksr1/4rDmL02miwi3olrh+k1ALjC0uv55RP0GgBcYel16vkQeg0AxbL0eu7GUXoNAK6w3l9/mvT6yIF49BoACmD22mhy93x92LVeA8D+Zen1mTtzRq9L8eg1ABQg1eukySLiLd47XaLXAOAIS68Xbp+k1wDgCkuv528eT3r9cSkevQaAApi9NposIt7Z26fpNQC4wtLr8spJeg0ArrD0+sytU/QaAFxh6fXinXl6DQCu2O35mmQDwB7o5L+/rkybv80YY8yJVaZFxFu4ORf3Wldn1UclxhhjTk1XZ7P3IVprVZku/DtjjDGWrDKtte593q7Za621rs5SbcYYK36VaV2d7YY5e39d0AU7AGCE7M83Fv39AAAGExHviweL9BoAHCci3vn7Z+k1ADhORP4DAOGlVPq1wwUAAAAASUVORK5CYII=" alt="" />
2. 实现代码
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <EditText
android:id="@+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入"/> </LinearLayout>
MainActivity.java
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private Drawable mIconSearchDefault; // 搜索文本框默认图标
private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标
private EditText mSearchView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 得到资源里面的图标文件
final Resources res = getResources();
// 默认的图标
mIconSearchDefault = res.getDrawable(R.drawable.txt_search_default);
// 清除图标
mIconSearchClear = res.getDrawable(R.drawable.txt_search_clear); mSearchView = (EditText) findViewById(R.id.txtSearch);
mSearchView.addTextChangedListener(tbxSearch_TextChanged);
mSearchView.setOnTouchListener(txtSearch_OnTouch); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} /**
* 判断输入框中是否有数据,然后显示相应的图标文件
*/
private TextWatcher tbxSearch_TextChanged = new TextWatcher() { // 缓存上一次文本框内是否为空
private boolean isnull = true; @Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s)) {
if (!isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
null, mIconSearchDefault, null);
isnull = true;
}
} else {
if (isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
null, mIconSearchClear, null);
isnull = false;
}
}
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
} /**
* 随着文本框内容改变动态改变列表内容
*/
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { }
}; // 当清除图标被点击的时候的处理事件
private OnTouchListener txtSearch_OnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
int curX = (int) event.getX();
if (curX > v.getWidth() - 38
&& !TextUtils.isEmpty(mSearchView.getText())) {
mSearchView.setText("");
int cacheInputType = mSearchView.getInputType();// backup
// the input
// type
mSearchView.setInputType(InputType.TYPE_NULL);// disable
// soft
// input
mSearchView.onTouchEvent(event);// call native handler
mSearchView.setInputType(cacheInputType);// restore input
Toast toast = Toast.makeText(MainActivity.this, "你好啊", Toast.LENGTH_SHORT);
toast.show(); // type
return true;// consume touch even }
break;
}
return false;
}
}; }
3. 使用的图片
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAbCAMAAABY1h8eAAAAM1BMVEW/v7+/v7+/v7+/v7////+/v7+/v7+/v7+/v7+/v7+/v7+/v7+/v7+/v7+/v7+/v7+/v7+r8GajAAAAEXRSTlMAMGCAACCP3/8Qz79An++vUBjdUD8AAADgSURBVCjPfZLhAsMQDITboI5ie/+nXY62bKvmFz7JnciyXLGKMUZW2+MkbvNo4Tf3Aw3GMCMMUU92cawgu65juCBZTEcNa1M8Kfe6yWG5oA1Z6QFVL3fPtV5uuqrCInyJa/p8C4Uc4QZQT1BWXikQpQnYCD12HqpHpcoA5u7wCvVUWI09KEJW2AThHS2HKlZzUPNpSZ2IpdejtWVg1tJvh6wBVDv/8C7zUfNw60p3G5rb9PBOPHXo9dXb9jGuSsZqe/or8eE/q+fJJLRe3M9QSdPpKyZM5/ZEHZ4T/x4n/gPXSQ3LnpwLngAAAABJRU5ErkJggg==" alt="" /> aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAABgFBMVEX////3/P+z6f+T4f941v+L3v+r5P/s+f/x+v8guv8Ytf8Ysv8dtf9hzv/X8v/K7P8tu/8twv8wxv8xzv8vyf8esP+e3/8hsv8t1P8dxP8ttv8otP8brP+W2v8usv86y/+F3/+l5/9Q0P8kw//G7f92zP8bpP78/v8gqf9Dtv8lpf4euP++5f8doPpOw/8fn/cxrv9+zf+O0/8cm/QdmPFau/8imvTF5f8ppP1JtP+G1P8gl+4yqv+43/zm9P8hmfMooPdWvf80qf9+xfmy3f4kmvIgk+ktnvbr9v9Eqfgsofs1pv6z4/9Pqe84qv8kl/JYsvj1+/95w/0ypv80tP/J5fwqmO84p/9Zs/wqqv+h2v8umvMrmfQ5pf45ovo3oPc5pf0umvQllO8mmPUznvc+sf9qwv8vl/A6pPw5oPY5nfI+rf8/uv88qf83ofo8tf8tpv8xk+ctlOw2rv+X0f8mid46mu47pP8uo/+d0/+Dxv9Ytf9wwP8tnv8+pf8xof/rr60gAAAAgHRSTlMA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1AKuiAAAAFRSURBVCjPY2CAA0YmZhZWNnYGNMDBwsnFzcPNzcXLhyLOL8AtKCgkLCIoIMojhiwuKi4gJCEpKSkhLCUtIwsXZ5cTlRKUlFdQZFaSVAbKqMAkVNWkpJVZ1UFMJiFxOQ1Ndah7tKTkRJmhilS05eTU+CFsHV05LT11mHYWfQNdQwjTyNjABOEUPhMDLVMI09DMwMQc4SMTAwtLCNPK2sbEFi5hZ2/j4Ahhstk7mTnDJVzsnVxVoWrcnJzcPWBB5unlreUD5fi6+/kHBELYQf7ewSEcEHZoWHh4uL+nrQdDoHmEf7hfZBRUPDomNi4+ITEpOSXZPyYuNTIN4qfQ9OiMzKzsHM3cjLyM3NT8gjSIoXaFRXFZWeEF2QU2+fn5xQV+UHMYSkrLysrKKyrVdaqqq6trXDhgzq6sravLrahkwAAesfXe9VjEgZZUVZkzUAEAAFLaP89l9e6lAAAAAElFTkSuQmCC" alt="" />
4.说明
在农民伯伯的博客中看到,因为找不到网址 ,特此说明一下