所以我正在创建一个将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();
}
}
}
}
答案 0 :(得分:3)
只需将红色,绿色,蓝色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]);
}