以编程方式将RGB转换为HEX – Android
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了以编程方式将RGB转换为HEX – Android,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2715字,纯文字阅读大概需要4分钟。
内容图文
![以编程方式将RGB转换为HEX – Android](/upload/InfoBanner/zyjiaocheng/703/cd59a6f3b3ce436d8180deae8a600fe8.jpg)
所以我正在制作一个将RGB颜色转换为十六进制的应用程序(例如#FFFFFF).我有三个红色,绿色和蓝色的编辑文本.当我输入每个编辑文本的值(如255,255,255)并单击该按钮时,RGB值将转换为十六进制,并将显示在textview中.有人帮我计算这里是我的代码.
public class MainActivity extends Activity {
public String str = "";
int i, num, numtemp;
EditText showRed, showGreen, showBlue;
String displayStr = "";
Button zero, one, two, three, four, five, six, seven, eight, nine, clear, convert;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
showRed = (EditText) findViewById(R.id.red);
showGreen = (EditText) findViewById(R.id.green);
showBlue = (EditText) findViewById(R.id.blue);
clear = (Button) findViewById(R.id.clear);
convert = (Button) findViewById(R.id.convert);
convert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Convertion Computaion here HELP!!!!!!!!!
}
});
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
str = "";
num = 0;
numtemp = 0;
showRed.setText("");
showRed.requestFocus();
showGreen.setText("");
showBlue.setText("");
displayStr = "";
}
});
}
public void btn1Clicked(View v) {
insert(1);
}
public void btn2Clicked(View v) {
insert(2);
}
public void btn3Clicked(View v) {
insert(3);
}
public void btn4Clicked(View v) {
insert(4);
}
public void btn5Clicked(View v) {
insert(5);
}
public void btn6Clicked(View v) {
insert(6);
}
public void btn7Clicked(View v) {
insert(7);
}
public void btn8Clicked(View v) {
insert(8);
}
public void btn9Clicked(View v) {
insert(9);
}
public void btn0Clicked(View v) {
insert(0);
}
private void insert(int j) {
// TODO Auto-generated method stub
if (showRed.hasFocus()) {
str = str + Integer.toString(j);
num = Integer.valueOf(str).intValue();
displayStr += Integer.toString(j);
showRed.setText(displayStr);
if ((showRed.length() == 3)) {
displayStr = "";
showGreen.requestFocus();
}
} else if (showGreen.hasFocus()) {
str = str + Integer.toString(j);
num = Integer.valueOf(str).intValue();
displayStr += Integer.toString(j);
showGreen.setText(displayStr);
if ((showGreen.length() == 3)) {
displayStr = "";
showBlue.requestFocus();
}
} else if (showBlue.hasFocus()) {
str = str + Integer.toString(j);
num = Integer.valueOf(str).intValue();
displayStr += Integer.toString(j);
showBlue.setText(displayStr);
if ((showBlue.length() == 3)) {
displayStr = "";
showBlue.clearFocus();
}
}
}
}
解决方法:
只需将红色,绿色,蓝色3种颜色的数组传递给此功能即可
int[] color={200,170,100};
btn.setBackgroundColor(getHexColor(color));
public static int getHexColor(int[] color) {
return android.graphics.Color.rgb(color[0], color[1], color[2]);
}
内容总结
以上是互联网集市为您收集整理的以编程方式将RGB转换为HEX – Android全部内容,希望文章能够帮你解决以编程方式将RGB转换为HEX – Android所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。