1.布局(真的不要吐槽这个)
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidstudy.EditTextMainActivity" >
<TextView
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<EditText
android:id="@+id/ed"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tip"
android:hint="在此处输入搜索内容"
android:gravity="top"
android:layout_alignParentLeft="true"
/>
<!-- 搜索 -->
<Button
android:id="@+id/btn_search"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_below="@+id/tip"
android:src="@drawable/edit_delete"
android:layout_alignParentRight="true"
android:text="search"
android:textSize="12dp"
/>
<!-- 清空编辑框内容 -->
<ImageButton
android:id="@+id/btn_delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@null"
android:layout_below="@+id/tip"
android:layout_marginRight="10dp"
android:src="@drawable/edit_delete"
android:layout_toLeftOf="@id/btn_search"
/>
<ListView
android:id="@+id/listview"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:divider="#666"
android:dividerHeight="1dp"
android:scrollbars="none"
android:layout_below="@+id/ed"
android:visibility="gone"
></ListView>
</RelativeLayout>
</span>
主要代码:
<span style="font-size:14px;">public class EditTextMainActivity extends Activity {
private TextView tip;
private EditText ed;
private ImageButton delete;
private Button search;//搜索
private String tipStr,tipEnd;
private int length=20;//允许输入的长度
private int numBefore,numAfter;//已经输入的字符数,还剩多少个字符可以输入,这里假设最多20个字符
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_edit_text_main);
tipStr= getResources().getString(R.string.sdit_tip);
//最终显示的字符数
tipEnd=String.format(tipStr, numBefore, numAfter);
tip=(TextView) findViewById(R.id.tip);
ed=(EditText) findViewById(R.id.ed);
delete=(ImageButton) findViewById(R.id.btn_delete);
search=(Button) findViewById(R.id.btn_search);
//给editText添加监听事件
ed.addTextChangedListener(mTextWatcher);
//清空输入内容
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ed.setText("");
}
});
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(EditTextMainActivity.this, "此时的搜索内容:"+ed.getText().toString(), 3000).show();
}
});
}
TextWatcher mTextWatcher = new TextWatcher() {
private CharSequence temp;
private int editStart ;
private int editEnd ;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("yyy", "onTextChanged");
temp = s;
if(temp.length()>0){
numBefore=temp.length();//已经输入的字符长度
numAfter=length-temp.length();
//最终显示的字符数
tipEnd=String.format(tipStr, numBefore, numAfter);
tip.setText(tipEnd);
delete.setVisibility(View.VISIBLE);
if(temp.length()>=length){
tip.setText("你输入的字数已经超过了限制!");
}
} else{
delete.setVisibility(View.GONE);
}
}
//好像没有啥用
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
// tip.setText(s);//将输入的内容实时显示
}
@Override
public void afterTextChanged(Editable s) {
Log.i("yyy", "afterTextChanged@最后获取的文字"+s.toString());
// TODO Auto-generated method stub
try {
editStart = ed.getSelectionStart();//开始的字符位置
editEnd = ed.getSelectionEnd();//结束的字符位置
if (temp.length() >length) {
s.delete(editStart-1, editEnd);//删除
int tempSelection = editStart;
ed.removeTextChangedListener(this);
ed.setText(s);
ed.addTextChangedListener(this);
ed.setSelection(tempSelection);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}</span>
最终效果: