android edittext用textwatcher监听,当手机输入小写字母时自动转为大写,求详细代码
public class MainActivity extends Activity {
EditText et ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) this.findViewById(R.id.editText1);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(this);
final String s1 = et.getText().toString();
//判断如果是小写的字母的换,就转换
if((s1.charAt(0))-0 >= 97 && (s1.charAt(0))-0 <=122){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//小写转大写
et.setText(s1.toUpperCase());
}
}, 300);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
android中两个输入框,如何获取这两个值的乘积赋值给textview,就如web中onkeyup事件,自动获取。
使用TextWatcher类来监视改动.
- privateTextWatcherwatcher=newTextWatcher(){
- @Override
- publicvoidafterTextChanged(Editables){
- //TODOAuto-generatedmethodstub
-
- }
- @Override
- publicvoidbeforeTextChanged(CharSequences,intstart,intcount,
- intafter){
- //TODOAuto-generatedmethodstub
-
- }
- @Override
- publicvoidonTextChanged(CharSequences,intstart,intbefore,
- intcount){
- Log.d("TAG","[TextWatcher][onTextChanged]"+s);
-
- }
-
- };
使用时
- EditTextet=(EditText)findViewById(R.id.edit);
- et.addTextChangedListener(watcher);
edittext监听是否有内容
监听EditText内容变化
1、给EditText追加ChangedListener
EditText editText = (EditText) findViewById(R.id.edittext);
editText.addTextChangedListener(watcher);
2、描述监听
private TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};