我想在edittext中有一个图像,这将在用户键入时通过动画改变它的可绘制性,如下所示:
谢谢 :)
你只需要设置CompoundDrawables {Right | 左 | 顶部 | xml 中的底部} 属性
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/searchedTxt"
android:width="200dp"
android:layout_alignParentRight="true"
android:drawableLeftt="@android:drawable/ic_menu_search"/>
放置EditText
并ImageView
在RelativeLayout
更改文本时更改图像
编辑:添加示例代码
1) main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="New EditText"
android:id="@+id/editText"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/imageView"
android:background="@drawable/first_image"
/>
</RelativeLayout>
2) MyActivity 类
public class MyActivity extends Activity {
private EditText editText;
private ImageView image;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeFormViews();
initializeFormEvents();
}
public void initializeFormViews(){
editText = (EditText) findViewById(R.id.editText);
image = (ImageView) findViewById(R.id.imageView);
}
public void initializeFormEvents(){
editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
// changes image after editing text in EditText
image.setBackgroundResource(R.drawable.second_image);
}
});
}
}
请使用下面给出的 xml 代码片段作为主 xml 文件的一部分(负责 UI):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget665"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:paddingLeft="1dip"
android:paddingRight="2dip">
<ImageView
android:id="@+id/searchImageView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@android:drawable/ic_search_category_default"
android:layout_gravity="center_vertical"
android:paddingTop="2dip"
android:paddingBottom="2dip">
</ImageView>
<EditText
android:id="@+id/search_edit_text"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:text=""
android:textSize="18sp"
android:background="#f5f5dc"
android:layout_gravity="center_vertical">
</EditText>
</LinearLayout>
将此内部类放在您的主要活动中
public class SearchTextWatcher implements TextWatcher{
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence prefix, int start, int before, int count) {
if(!prefix.equals("")){
changeImageToProcessing();
}else{
changeImageToIdle();
}
}
}
在 onCreate() 期间从 contentView 中提取 searchBar (EditText) 和 iv (ImageView),类似于
searchBar = (EditText)contentView.findViewById(R.id.customer_search_edit_text);
iv = (ImageView)contentView.findViewById(R.id.searchImageView);
将文本更改监听器附加到搜索栏编辑文本
searchBar.addTextChangedListener(searchTextWatcher);
添加几个帮助方法,他们将负责相应地更改图像
public void changeImageToProcessing(){
iv.setImageResource(resId);// resid is the id of the working image
}
public void changeImageToIdle(){
iv.setImageResource(resId);// resid is the id of the idle image
}
//这些方法是从 SearchTextWatcher 类中的 onTextChange() 调用的