我希望你能幫助我解決這個問題。我可以生成隨機數字,但不是精確計數。隨機7位數字點擊android
問題是,一旦按鈕被點擊,我該如何生成7個隨機數字?
請參考下我的代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Random myRandom = new Random();
Button buttonGenerate = (Button)findViewById(R.id.generateme);
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
}
});
}
}
這裏是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random number"
android:id="@+id/generateme"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/generatenumber"
/>
</LinearLayout
爲什麼你不能把隨機數發生代碼放在一個'for'中,它一次執行7次? – SudoRahul 2013-03-11 04:57:56