我将创建一个应用,该应用将从数组String [] powerBalls
中使用5个数字随机提取3个数字,并将带有图片的图片切换到每个数字,然后显示该数字和图片(例如字符串“ 1”来自阵列powerballs
的图像将具有名称“ o1”)。问题是,只要开关盒能找到正确的图像,我就可以在显示屏上看到数字并看到图片。然后该应用程序关闭。图像始终可见,并且应该仅以随机数出现。此代码有什么问题?
我真的不知道我在做什么错,也找不到该程序的示例。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zufallsgenerator);
bindViews();
mGenerateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
generateRandomNumbers();
}
});
}
public void generateRandomNumbers(){
Collections.shuffle(Arrays.asList(powerBalls));
mFirstNumber.setText(powerBalls[0]);
mSecondNumber.setText(powerBalls[1]);
mThirdNumber.setText(powerBalls[2]);
mFourthNumber.setText(powerBalls[3]);
mFifthNumber.setText(powerBalls[4]);
mSixthNumber.setText(powerBalls[5]);
switch (powerBalls[0]) {
case "1":
imageViewDice1.setImageResource(R.drawable.o1);
imageViewDice1 = (ImageView)findViewById(R.id.image_view_dice1);
break;
case "2":
imageViewDice2.setImageResource(R.drawable.o2);
imageViewDice2 = (ImageView)findViewById(R.id.image_view_dice2);
break;
case "3":
imageViewDice3.setImageResource(R.drawable.o3);
imageViewDice3 = (ImageView)findViewById(R.id.image_view_dice3);
break;
case "4":
imageViewDice4.setImageResource(R.drawable.o4);
imageViewDice4 = (ImageView)findViewById(R.id.image_view_dice4);
break;
case "5":
imageViewDice5.setImageResource(R.drawable.o5);
imageViewDice5 = (ImageView)findViewById(R.id.image_view_dice5);
break;
case "6":
imageViewDice6.setImageResource(R.drawable.o6);
imageViewDice6 = (ImageView)findViewById(R.id.image_view_dice6);
break;
}
switch (powerBalls[1]) {
case "1":
imageViewDice1.setImageResource(R.drawable.o1);
imageViewDice1 = (ImageView)findViewById(R.id.image_view_dice1);
break;
case "2":
imageViewDice2.setImageResource(R.drawable.o2);
imageViewDice2 = (ImageView)findViewById(R.id.image_view_dice2);
break;
case "3":
imageViewDice3.setImageResource(R.drawable.o3);
imageViewDice3 = (ImageView)findViewById(R.id.image_view_dice3);
break;
case "4": // and so on...*****
,并在java类的末尾:
public void bindViews(){
mFirstNumber = (TextView)findViewById(R.id.firstNumber);
mSecondNumber = (TextView)findViewById(R.id.secondNumber);
mThirdNumber = (TextView)findViewById(R.id.thirdNumber);
mFourthNumber = (TextView)findViewById(R.id.fourthNumber);
mFifthNumber = (TextView)findViewById(R.id.fifthNumber);
mSixthNumber = (TextView)findViewById(R.id.sixthNumber);
mPowerBall = (TextView)findViewById(R.id.powerBall);
mGenerateButton = (Button)findViewById(R.id.mgenerateButton);
带有视图的xml文件的示例:
<TextView
android:id="@+id/firstNumber"
android:layout_weight="1"
android:fontFamily="serif"
android:textSize="24sp"
android:padding="4dp"
android:layout_margin="8dp"
android:text="01"
android:textColor="@color/startblue"
android:layout_width="0dp"
android:layout_height="wrap_content" />
在此先感谢您的建议和答案。干杯