Android进阶之路
来源:互联网 发布:淘宝里的1元秒杀 编辑:程序博客网 时间:2024/06/05 08:54
My God? 因为我们与IOS的不同,当我们需要清空EditText内容时,有时候无法进行清理,故现在才发现很早以前就已经有了处理方式,一种为原生处理,一种为自定义控件。
如需在输入完成后显示删除按钮,可监听EditText焦点和TextWatch .
MainActivity :
package com.bakheet.effect.cleanedittext;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;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 TextView mBtn; private ClearEditText mEdit; private EditText mEt_1; private ImageView mIv_1; private TextView mBtn_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 第一种方式 - 原始控件 * */ mBtn_1 = (TextView) findViewById(R.id.btn_1); mIv_1 = (ImageView) findViewById(R.id.iv_clear); mEt_1 = (EditText) findViewById(R.id.et_1); mIv_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mEt_1.setText(""); } }); mBtn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (TextUtils.isEmpty(mEt_1.getText().toString().trim())) { //点击之后,失去焦点才会触发,没特殊要求就不必要使用了 //mEdit.setShakeAnimation(); Toast.makeText(MainActivity.this, "您输入的内容为空", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "一个月做完一个项目,有点难 > < !", Toast.LENGTH_SHORT).show(); } } }); /** * 第二种方式 * */ mEdit = (ClearEditText) findViewById(R.id.edit); mBtn = (TextView) findViewById(R.id.btn); mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String data = mEdit.getText().toString().trim(); if (TextUtils.isEmpty(data)) { //点击之后,失去焦点才会触发,没特殊要求就不必要使用了 //mEdit.setShakeAnimation(); Toast.makeText(MainActivity.this, "您输入的内容为空", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "一个月做完一个项目,有点难 > < !", Toast.LENGTH_SHORT).show(); } } }); }}
MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="vertical" tools:context="com.bakheet.effect.cleanedittext.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#999999" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" > <EditText android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:padding="5dp" android:background="@null" android:id="@+id/et_1" android:layout_centerVertical="true" android:textSize="13sp" android:hint="原始的Edit" /> <ImageView android:id="@+id/iv_clear" android:layout_width="15dp" android:layout_height="15dp" android:src="@drawable/icon_clear_1" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_gravity="center" android:layout_marginRight="20dp" /> </LinearLayout> <TextView android:id="@+id/btn_1" android:layout_marginTop="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="30dp" android:text="确定" android:background="#963" android:textColor="#fff" android:padding="5dp" android:gravity="center" /> <com.bakheet.effect.cleanedittext.ClearEditText android:id="@+id/edit" android:layout_width="match_parent" android:background="#999999" android:drawableLeft="@drawable/icon_edit" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:singleLine="true" android:textSize="13sp" android:drawableRight="@drawable/icon_clear_2" android:paddingRight="5dp" android:hint="Clean 测试" android:textColor="#fff" android:layout_height="30dp" /> <TextView android:id="@+id/btn" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="30dp" android:text="确定" android:background="#963" android:textColor="#fff" android:padding="5dp" android:gravity="center" /></LinearLayout>
第一种方式来源于OurSelf.
第二种方式来源于 :http://blog.csdn.net/xiaanming/article/details/11066685
此Demo完整代码,下载链接!
阅读全文
0 0
- Android进阶之路
- android进阶之路
- Android 进阶之路
- Android 进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- 人群计数--Cross-scene Crowd Counting via Deep Convolutional Neural Networks
- 线程和线程池
- JS内置对象2——字符串
- Servlet.init()问题
- django 模板文件路径设置
- Android进阶之路
- date_range
- 带你快速领略8个排序算法(C/C++实现)
- OpenCV霍夫系列(后篇)-统计概率霍夫变换(HoughLinesP)
- 通过示例学习Python列表推导
- STL之效率比较
- C++ Assert()断言机制原理以及使用
- web上传文件方法
- Android播放器开发基础(二)