Convert RGB to HEX programatically - Android

2019-01-29 11:38发布

问题:

So Im making an app that converts RGB Colors to Hexadecimal (ex. #FFFFFF). I have three edittext for Red, Green and Blu. When I input values for every edittext like (255,255,255) and I click the button the RGB values will be converted to Hexadecimal and will be shown in textview. Can someone help me with the computation here is my codes.

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();

            }
    }                                    
}        
}

回答1:

Just pass an array of 3 colors red, green, blue to this function

  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]);
 }


标签: android hex rgb