2014-04-01 48 views
0

因此,我正在製作將RGB顏色轉換爲十六進制(例如#FFFFFF)的應用程序。我有三個紅色,綠色和藍色的編輯文本。當我輸入像(255,255,255)這樣的每個編輯文本的值時,單擊該按鈕,RGB值將被轉換爲十六進制,並將顯示在textview中。有人可以幫我計算這裏是我的代碼。以編程方式將RGB轉換爲HEX - Android

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

http://developer.android.com/reference/android/graphics/Color.html – pskink

+0

@pskink我需要計算我的代碼的幫助:(請 – jandroid

+0

我給你一個鏈接到一個類,有方法將紅色,綠色和藍色轉換爲int顏色,你讀過嗎? – pskink

回答

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