import java.text.DecimalFormat; public void changeColor(View view) { DecimalFormat decimalFormat = new DecimalFormat("000"); //獲取隨機數對象,產生三個隨機數值(RGB值) Random x = new Random(); int red = x.nextInt(256); String sred = decimalFormat.format(red); txvR.setText("紅:"+sred); txvR.setTextColor(Color.rgb(red,0,0)); int green = x.nextInt(256); String sgreen = decimalFormat.format(green); txvG.setText("綠:"+sgreen); txvG.setTextColor(Color.rgb(0,green,0)); int blue = x.nextInt(256); String sblue = decimalFormat.format(blue); txvB.setText("藍:"+sblue); txvB.setTextColor(Color.rgb(0,0,blue)); //設置界面最上方的按鈕 button 的文字顏色 button.setTextColor(Color.rgb(red,green,blue)); //設置界面最下方的空白 LinearLayout 的背景顏色 colorBlock.setBackgroundColor(Color.rgb(red,green,blue)); }
import java.text.DecimalFormat; //如果數字1是字符串,如下處理: String string="1"; DecimalFormat decimalFormat =new DecimalFormat("0000"); String string2=decimalFormat.format(Integer.parseInt(str1)); System.out.println(string2); //如果數字1是整型,如下處理: int string=1; DecimalFormat decimalFormat =new DecimalFormat("0000"); String string2=decimalFormat.format(string); System.out.println(string2);