package com.example.testfocus; import com.example.testfocus.OtherActivity.EditTextManagerWatcher; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnFocusChangeListener; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class Other extends LinearLayout implements OnFocusChangeListener{ private EditText editText1; private EditText editText2; private EditText editText3; private TextView test1; private TextView test2; private TextView test3; public Other(Context context) { super(context); // TODO Auto-generated constructor stub } public Other(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.other, this, true); init(); } private void init() { editText1 =(EditText) findViewById(R.id.editText1); editText2 =(EditText) findViewById(R.id.editText2); editText3 =(EditText) findViewById(R.id.editText3); test1 =(TextView) findViewById(R.id.test1); test2 =(TextView) findViewById(R.id.test2); test3 =(TextView) findViewById(R.id.test3); test1.setTextColor(this.getResources().getColor(R.color.other)); editText1.setOnFocusChangeListener(this); editText2.setOnFocusChangeListener(this); editText3.setOnFocusChangeListener(this); } /** * 设置焦点位置 */ @Override public void onFocusChange(View v, boolean hasFocus) { Editable editable; int len; switch (v.getId()) { case R.id.editText1: if(hasFocus){ editText1.addTextChangedListener(new EditTextManagerWatcher(3, editText1)); test1.setTextColor(getResources().getColor(R.color.other)); }else{ test1.setTextColor(getResources().getColor(R.color.white)); } break; case R.id.editText2: if(hasFocus){ editText2.addTextChangedListener(new EditTextManagerWatcher(3, editText2)); test2.setTextColor(getResources().getColor(R.color.other)); } else{ test2.setTextColor(getResources().getColor(R.color.white)); } break; case R.id.editText3: if(hasFocus){ editText3.addTextChangedListener(new EditTextManagerWatcher(3, editText3)); test3.setTextColor(getResources().getColor(R.color.other)); }else{ test3.setTextColor(getResources().getColor(R.color.white)); } break; } } /** * * <P> [功能] 监听输入内容是否超出最大长度,控制输入类型,</P> * <P> [说明] </P> * <P> [备注] </P> * @author nWX212045 * @2014-03-3 上午09:48:27 */ public class EditTextManagerWatcher implements TextWatcher { //最大长度 private int maxLen; //监听改变的文本框 private EditText editText; /** * 构造函数 */ public EditTextManagerWatcher(int maxLen,EditText editText){ this.maxLen = maxLen; this.editText = editText; } @Override public void onTextChanged(CharSequence ss, int start, int before, int count) { Editable editable = editText.getText(); int len = editable.length(); //大于最大长度 if(len == maxLen){ /*int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); //截取新字符串 String newStr = str.substring(0, maxLen); editText.setText(newStr); editable = editText.getText(); //新字符串长度 int newLen = editable.length(); //旧光标位置超过字符串长度 if(selEndIndex > newLen){ selEndIndex = editable.length(); } //设置新的光标所在位置 Selection.setSelection(editable, selEndIndex);*/ if(editText==editText1){ editText2.setFocusable(true); editText2.setFocusableInTouchMode(true); editText2.requestFocus(); //test1.setTextColor(getResources().getColor(R.color.white)); test2.setTextColor(getResources().getColor(R.color.other)); }else{ if(editText==editText2){ editText3.setFocusable(true); editText3.setFocusableInTouchMode(true); editText3.requestFocus(); //test2.setTextColor(getResources().getColor(R.color.white)); test3.setTextColor(getResources().getColor(R.color.other)); } } }else if(len>maxLen){ return; } } @Override public void afterTextChanged(Editable s) { try { String temp = s.toString(); String tem = temp.substring(temp.length()-1, temp.length()); char[] temC = tem.toCharArray(); int mid = temC[0]; if(mid>=48&&mid<=57){//数字 return; } /*if(mid>=65&&mid<=90){//大写字母 return; } if(mid>97&&mid<=122){//小写字母 return; }*/ //s.delete(temp.length()-1, temp.length()); } catch (Exception e) { // TODO: handle exception } } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { } } }<com.example.testfocus.Other android:layout_width="match_parent" android:layout_height="wrap_content" ></com.example.testfocus.Other>
版权声明:本文为博主原创文章,未经博主允许不得转载。