<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/main_et" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/main_tv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>一、实现监听文本框字数,提示还能输入多少个字符
package chay.mian; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText editText; private TextView tip; private final int charMaxNum = 10; // 允许输入的字数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { editText = (EditText) findViewById(R.id.main_et); editText.addTextChangedListener(new EditChangedListener()); tip = (TextView) findViewById(R.id.main_tv); tip.setText("0/"+charMaxNum); } class EditChangedListener implements TextWatcher { private CharSequence temp; // 监听前的文本 private int editStart; // 光标开始位置 private int editEnd; // 光标结束位置 // 输入文本之前的状态 @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { temp = s; } // 输入文字中的状态,count是一次性输入字符数 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // if (charMaxNum - s.length() <= 5) { // tip.setText("还能输入" + (charMaxNum - s.length()) + "字符"); // } tip.setText((s.length()) + "/" + charMaxNum); } // 输入文字后的状态 @Override public void afterTextChanged(Editable s) { /** 得到光标开始和结束位置 ,超过最大数后记录刚超出的数字索引进行控制 */ editStart = editText.getSelectionStart(); editEnd = editText.getSelectionEnd(); if (temp.length() > charMaxNum) { // Toast.makeText(getApplicationContext(), "最多输入10个字符", Toast.LENGTH_SHORT).show(); s.delete(editStart - 1, editEnd); editText.setText(s); editText.setSelection(s.length()); } } }; }
二、数据类型检验,当输入不是整型数字的时候,会立刻弹出输入框,提示改正
MainActivity:
package chay.mian; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText editText; private TextView tip; String str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mian); init(); } private void init() { editText = (EditText) findViewById(R.id.mian_et); editText.addTextChangedListener(new EditChangedListener()); tip = (TextView) findViewById(R.id.mian_tv); tip.setText("请输入整型数字"); } class EditChangedListener implements TextWatcher { // 输入文本之前的状态 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("TAG", "beforeTextChanged--------------->"); } // 输入文字中的状态,count是一次性输入字符数 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TAG", "onTextChanged--------------->"); } // 输入文字后的状态 @Override public void afterTextChanged(Editable s) { Log.d("TAG", "afterTextChanged--------------->"); str = editText.getText().toString(); try { // if ((editText.getText().toString()) != null) Integer.parseInt(str); } catch (Exception e) { showDialog(); } } }; private void showDialog() { AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error); builder.setMessage("你输出的整型数字有误,请改正"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog = builder.create(); dialog.show(); } }
原文地址:http://blog.csdn.net/ycwol/article/details/46594275