馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
本帖最後由 zzhsu 於 2017-8-24 00:51 編輯
小弟剛接觸android app兩個禮拜
在書中看到一個程式碼的寫法
- <pre style="">public class MainActivity extends AppCompatActivity {
- TextView txvR,txvB,txvG;
- View colorBlock;
- Button btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txvR = (TextView) findViewById(R.id.RedCode);
- txvB = (TextView) findViewById(R.id.BlueCode);
- txvG = (TextView) findViewById(R.id.GreenCode);
- colorBlock = findViewById(R.id.ColorBlockTextView);
- btn = (Button) findViewById(R.id.button);
- }
- public void onChangeColor(View v){
- Random x = new Random();
-
- int red = x.nextInt(256);
- txvR.setText(""+red);
- txvR.setTextColor(Color.rgb(red,0,0));
- int blue = x.nextInt(256);
- txvB.setText(""+blue);
- txvB.setTextColor(Color.rgb(0,0,blue));
- int green = x.nextInt(256);
- txvG.setText(""+green);
- txvG.setTextColor(Color.rgb(0,green,0));
- colorBlock.setBackgroundColor(Color.rgb(red,green,blue));
- btn.setBackgroundColor(Color.rgb(red,green,blue));
- }
- }</pre>
複製代碼
以下程式碼一直重複
- int green = x.nextInt(256);
- txvG.setText(""+green);
- txvG.setTextColor(Color.rgb(0,green,0));
複製代碼
在java中,可以把以上代碼自訂一個method後,重覆使用
但在android中,不知如何實現自訂一個method後,重覆使用
在此請教各位前輩,不吝指教,感激不盡...
|
![](source/plugin/apk_quickrate/images/kspf.gif)
|