在TextView上面设置某一个字的字体颜色为指定颜色时,可以通过java类SpannableString类和Html语言来实现。
(一)SpannableString类方式
private void setText(TextView t){
String text = t.getText().toString().trim();
SpannableString span = new SpannableString(text);
span.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setText(span);
}
(二)html标记语言的方式
String html1 = "
*姓名:";
textView.setText(Html.fromHtml(html1));
当输入小写字母时,EditText编辑框自动转换成大写字母,也有多种方式,即给某个EditText设置监听、调用EditText对象的setTransformationMethod()方法。
(一)为EditText设置监听
mEdtLicensePlateNumber.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
mEdtLicensePlateNumber.removeTextChangedListener(this);//解除文字改变事件
mEdtLicensePlateNumber.setText(s.toString().toUpperCase());//转换
mEdtLicensePlateNumber.setSelection(s.toString().length());//重新设置光标位置
mEdtLicensePlateNumber.addTextChangedListener(this);//重新绑
licensePlateNumber = mEdtLicensePlateNumber.getText().toString().trim();
}
@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
}
});
(二)调用EditText对象的setTransformationMethod()方法
public class InputLowerToUpper extends ReplacementTransformationMethod{
@Override
protected char[] getOriginal() {
char[] lower = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
return lower;
}
@Override
protected char[] getReplacement() {
char[] upper = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
return upper;
}
}
editext.setTransformationMethod(new InputLowerToUpper());