功能描述:EditText手动输入字符串“#FFFFFF”等,改变TextView字体的颜色。
方法:
int android.graphics.Color.parseColor(String colorString)
public static intparseColor (String colorString)
Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are:#RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'
红字部分是函数parseColor 参数支持的类型。如果String是“#asffds”等不能解析的值,程序不会崩溃,log打印出IllegalArgumentException。
代码非常简单,如下:
public class MainActivity extends Activity {
private EditText input;
private TextView text;
private Button change;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input_color);
text = (TextView) findViewById(R.id.text);
change = (Button) findViewById(R.id.change);
change.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = input.getText().toString();
int color = Color.parseColor(s);
text.setTextColor(color);
}
};
}
源代码:http://download.csdn.net/detail/liucaoye/8128019