我想制作七个数字幸运抽奖的应用程序。这是我的代码。但我不知道为什么当我多次单击这两个按钮时应用程序将停止。单击“清除”后,“下一个数字”按钮将无响应。 有谁知道为什么?
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView[] arr = new TextView[7];
int[] num = new int[6];
int count = 0;
TextView number_7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arr[0] = ((TextView) findViewById(R.id.tv_number_one));
arr[1] = ((TextView) findViewById(R.id.tv_number_two));
arr[2] = (TextView)findViewById(R.id.tv_number_three);
arr[3] = (TextView)findViewById(R.id.tv_number_four);
arr[4] = (TextView)findViewById(R.id.tv_number_five);
arr[5] = (TextView)findViewById(R.id.tv_number_six);
number_7 = (TextView)findViewById(R.id.tv_number_seven);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void generate(View v)
{
Random myRandom = new Random();
if (count >= 7) {
clear(arr[0]);
clear(arr[1]);
clear(arr[2]);
clear(arr[3]);
clear(arr[4]);
clear(arr[5]);
clear(arr[6]);
for (TextView tv : arr) tv.setText("?");
}
switch (count){
case(0):{
num[0]= myRandom.nextInt(48) + 1;
arr[0].setText(String.valueOf(num[0]));
break;
}
case(1):{
num[1]= myRandom.nextInt(48) + 1;
arr[1].setText(String.valueOf(num[1]));
break;
}
case(2):{
num[2]= myRandom.nextInt(48) + 1;
arr[2].setText(String.valueOf(num[2]));
break;
}
case(3):{
num[3]= myRandom.nextInt(48) + 1;
arr[3].setText(String.valueOf(num[3]));
break;
}
case(4):{
num[4]= myRandom.nextInt(48) + 1;
arr[4].setText(String.valueOf(num[4]));
break;
}
case(5):{
num[5]= myRandom.nextInt(48) + 1;
arr[5].setText(String.valueOf(num[5]));
break;
}
case(6):{
int num_7;
Arrays.sort(num);
num_7= myRandom.nextInt(48) + 1;
arr[0].setText(String.valueOf(num[0]));
arr[1].setText(String.valueOf(num[1]));
arr[2].setText(String.valueOf(num[2]));
arr[3].setText(String.valueOf(num[3]));
arr[4].setText(String.valueOf(num[4]));
arr[5].setText(String.valueOf(num[5]));
number_7.setText(String.valueOf(num_7));
break;
}
}
count++;
}
public void clear(View v){
TextView num_1 = (TextView)findViewById(R.id.tv_number_one);
num_1.setText("?");
TextView num_2 = (TextView)findViewById(R.id.tv_number_two);
num_2.setText("?");
TextView num_3 = (TextView)findViewById(R.id.tv_number_three);
num_3.setText("?");
TextView num_4 = (TextView)findViewById(R.id.tv_number_four);
num_4.setText("?");
TextView num_5 = (TextView)findViewById(R.id.tv_number_five);
num_5.setText("?");
TextView num_6 = (TextView)findViewById(R.id.tv_number_six);
num_6.setText("?");
TextView num_7 = (TextView)findViewById(R.id.tv_number_seven);
num_7.setText("?");
}
}
activity_main
<TextView android:text="\?" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_number_one"
android:textSize="30sp"
android:textIsSelectable="true"
android:layout_marginLeft="10dp"
android:textColor="#67ceff"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_two"
android:layout_alignBottom="@+id/tv_number_one"
android:layout_toRightOf="@+id/tv_number_one"
android:layout_toEndOf="@+id/tv_number_one"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_three"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_two"
android:layout_toEndOf="@+id/tv_number_two"
android:layout_marginLeft="20dp"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_four"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_three"
android:layout_toEndOf="@+id/tv_number_three"
android:textColor="#67ceff"
android:textSize="30sp"
android:textIsSelectable="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_five"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_four"
android:layout_toEndOf="@+id/tv_number_four"
android:layout_marginLeft="20dp"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_six"
android:layout_alignBottom="@+id/tv_number_five"
android:layout_toRightOf="@+id/tv_number_five"
android:layout_toEndOf="@+id/tv_number_five"
android:textColor="#67ceff"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/tv_number_seven"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/tv_number_six"
android:layout_toEndOf="@+id/tv_number_six"
android:layout_marginLeft="20dp"
android:textColor="#6198ff"
android:textSize="30sp"
android:layout_marginStart="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT NUMBER"
android:id="@+id/button_next_num"
android:layout_below="@+id/tv_number_one"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:onClick="generate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:id="@+id/button_clear"
android:onClick="clear"
android:layout_alignTop="@+id/button_next_num"
android:layout_toRightOf="@+id/tv_number_six"
android:layout_toEndOf="@+id/tv_number_six"
android:layout_marginLeft="30dp" />
答案 0 :(得分:0)
编辑:根据您更新的问题
增加count
作为你做的最后一件事。
// check if the count is too high first
if (count >= 7) {
// desired behaviour if count is too high
// clear(); //?
}
num[count] = myRandom.nextInt(48) + 1;
views[count].setText(String.valueOf(num[count]));
count++;
<强>原始强>
抱歉,我一开始并不理解这个问题。
我会创建一个TextView
列表,并维护一个应用程序所在数字的计数器。
TextView[] views = new TextView[7];
int[] randomNumbers = new int[7];
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
views[0] = (TextView) findViewById(R.id.tv_number_one);
views[1] = (TextView) findViewById(R.id.tv_number_two);
// etc, etc
}
下一个按钮监听器
@Override
public void onClick(View v) {
// check if there are any more numbers required (don't want to cause a NPE)
// generate a new random number
// check if it is unique in the list
// if not generate a new number and repeat
// when you have a unique number save it to
// randomNumbers[counter] and display it in views[counter]
// increment the counter
}
清除按钮侦听器
@Override
public void onClick(View v) {
// clear list, clear views, reset counter
randomNumbers = new int[7];
for (TextView tv : views) tv.setText("?");
counter = 0;
}