首先要找到相对应的控件,之后再来一个数组,再把数组里面的数字显示到控件上面。
private void initView() { mNum0 = (Button) findViewById(R.id.button0); mNum1 = (Button) findViewById(R.id.button1); mNum2 = (Button) findViewById(R.id.button2); mNum3 = (Button) findViewById(R.id.button3); mNum4 = (Button) findViewById(R.id.button4); mNum5 = (Button) findViewById(R.id.button5); mNum6 = (Button) findViewById(R.id.button6); mNum7 = (Button) findViewById(R.id.button7); mNum8 = (Button) findViewById(R.id.button8); mNum9 = (Button) findViewById(R.id.button9); //洗牌 int[] num = new int[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; shuffleSort(num); mNum0.setText(String.valueOf(num[0])); mNum1.setText(String.valueOf(num[1])); mNum2.setText(String.valueOf(num[2])); mNum3.setText(String.valueOf(num[3])); mNum4.setText(String.valueOf(num[4])); mNum5.setText(String.valueOf(num[5])); mNum6.setText(String.valueOf(num[6])); mNum7.setText(String.valueOf(num[7])); mNum8.setText(String.valueOf(num[8])); mNum9.setText(String.valueOf(num[9]));
而这些一开始都是按一定的顺序排好的。
//洗牌算法 private void shuffleSort(int[] data) { for (int i = 0; i < data.length - 1; i++) { int j = (int) (data.length * Math.random()); swap(data, i, j); } } private void swap(int[] data, int i, int j) { if (i == j) { return; } data[i] = data[i] + data[j]; data[j] = data[i] - data[j]; data[i] = data[i] - data[j]; }