编写不易,如有转载,请声明出处:http://blog.youkuaiyun.com/zxc514257857/article/details/73530704
Demo展示图片
关键代码
- 点击叉叉置空EditText
mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText("");
}
});
- EditText的监听,如果有内容显示叉叉,如果没内容隐藏叉叉
mEditText.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) {
if (s.length() == 0) {
mDelete.setVisibility(View.GONE);
} else {
mDelete.setVisibility(View.VISIBLE);
}
}
});
- 搜索的水波纹效果实现
// (layout)activity_main.xml
<TextView
android:id="@+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="搜索"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#fff"
android:background="@drawable/ripple_bg">
---------------------------------------------------------------------------------------------------
// (drawable)ripple_bg.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorAccent">
</ripple>
注:为了实现可自定义颜色的无界水波纹效果,将 minSdkVersion 调到了21,也就是没有适配5.0以下,如果要适配5.0以下,可参考:Android开发中的水波纹效果实现 http://blog.youkuaiyun.com/zxc514257857/article/details/73200900
全部代码
// (layout)activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zhumei.deletedemo.MainActivity">
<LinearLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#3F51B5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="34dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_weight="5">
<EditText
android:id="@+id/edittext"
android:singleLine="true"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/search_frame"
android:maxLength="10"
android:hint="请输入商品名称..."/>
<ImageView
android:id="@+id/delete"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:background="@drawable/delete"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="@+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="搜索"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#fff"
android:background="@drawable/ripple_bg">
</TextView>
</LinearLayout>
</RelativeLayout>
---------------------------------------------------------------------------------------------------
// (drawable)ripple_bg.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorAccent">
</ripple>
---------------------------------------------------------------------------------------------------
// MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
private ImageView mDelete;
private TextView mTextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
public void initView(){
mEditText = (EditText) findViewById(R.id.edittext);
mDelete = (ImageView) findViewById(R.id.delete);
mTextview = (TextView) findViewById(R.id.textview);
}
public void initData(){
mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setText("");
}
});
mEditText.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) {
if (s.length() == 0) {
mDelete.setVisibility(View.GONE);
} else {
mDelete.setVisibility(View.VISIBLE);
}
}
});
mTextview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "搜索!", Toast.LENGTH_SHORT).show();
}
});
}
}
Demo下载请移步:http://download.youkuaiyun.com/detail/zxc514257857/9876463
----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------