android 设置字体颜色、EditText自动输入转换成大写字母的多种方式

在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 = "<html><body><a><font color=\"#ff0000\">*</a>姓名:</body></html>";
		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());

android 设置字体颜色、EditText自动输入转换成大写字母的多种方式,布布扣,bubuko.com

时间: 2024-05-30 09:33:52

android 设置字体颜色、EditText自动输入转换成大写字母的多种方式的相关文章

toupper(将小写字母转换成大写字母)

/*toupper(将小写字母转换成大写字母) 相关函数 isalpha,tolower 表头文件 #include<ctype.h> 定义函数 int toupper(int c); 函数说明 若参数c为小写字母则将该对映的大写字母返回. 返回值 返回转换后的大写字母,若不须转换则将参数c值返回. 附加说明 范例 */ /* 将s字符串内的小写字母转换成大写字母 */ #include<ctype.h> main() { char s[]="aBcDeFgH12345;

将用户输入的字符串反向输出到页面上,并且要求将其中的小写字母转换成大写字母。

<script>var str = prompt("脚本提示:\n 请输入一行文字:","");var upper_str = str.toUpperCase();for(var i=upper_str.length-1;i>=0;i--) document.write(upper_str.charAt(i));</script>

编写程序,输入一个字符,判断它是否为小写字母,如果是,将它转换成大写字母,否则,不转换

package com.my.demo; import java.util.Scanner; public class Test1 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); System.out.println("请输入一串字母:"); String line=sc.nextLine(); char[] arr=line.toCharArray(); for(int i=0;

gedit embeded terminal 设置字体 颜色

# -*- coding: utf8 -*- # terminal.py - Embeded VTE terminal for gedit # This file is part of gedit # # Copyright (C) 2005-2006 - Paolo Borelli # # gedit is free software; you can redistribute it and/or modify # it under the terms of the GNU General P

备忘: Android中把EditText的输入内容转为大写

editText_SerialCode = (EditText) findViewById(R.id.editText_SerialCode); editText_SerialCode.addTextChangedListener(textWatcher); private TextWatcher textWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int

android EditText 限制输入大小写,大写转小写

在写登录验证的时候,需要将用户输入的大写字符转化为小写.看了下API,只有InputFilter.AllCaps是将EditText 的小写英文字符转化为大写.好吧.依赖AllCaps在写个类似的InputFilter吧.基本上Copy AllCaps的代码,只改了两行.大家看代码吧 import android.text.InputFilter; import android.text.SpannableString; import android.text.Spanned; import a

MarkDown设置字体颜色

记录一下如何设置字体颜色和大小 <font color=red size=5>gray</font> <font color="#4590a3" size="6px">文字</font> 效果如下: red 文字 原文地址:https://www.cnblogs.com/tenny-peng/p/11532668.html

DB2中如何将非自动存储转换成自动存储空间管理方式

DB2 10引入根据温度管理数据存储的表空间更管理方式,但是需要数据库启用自动存储管理,具体在存储路径在某个目录下即可,要讲非自动存储的表空间转换成自动存储的管理方式需要两个步骤,尤其是从低版本升级过来的数据库. ALTER DATABASE EMPLOYEE ADD STORAGE ON '/data' 这样即可,但是对于原来使用DMS或SMS方式管理的表空间需要做重定向恢复,其实就是先做全备份,然后指定redirect restore即可,在原来的实例下恢复: RESTORE DATABAS

【c语言】 输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是不转换

// 输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是不转换 #include <stdio.h> int main() { char ch; printf("请输入一个字符:"); scanf("%c",&ch); if(ch >= 'A' && ch <= 'Z') ch = ch + 32; printf("转换成小写字母是:%c\n",ch); return 0; }