public class MainActivity extends Activity {
Button button1, button2, okbutton;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.letterbutton);
button2 = (Button) findViewById(R.id.letter2button);
okbutton = (Button) findViewById(R.id.okbutton);
imageView = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
int number;
Random random = new Random();
number = random.nextInt(2) + 1;
while (number > 0) {
switch (number) {
case 1:
imageView.setBackgroundResource(R.drawable.image_2);
button1.setText("Pavel");
button2.setText("robin");
break;
case 2:
imageView.setBackgroundResource(R.drawable.image_3);
button1.setText("Galib");
button2.setText("Rimon");
break;
}
if (okbutton.isPressed()) {
number = random.nextInt(2) + 1;
}
}
}
}
当我运行此应用程序时,手机中没有显示任何内容。 但是没有错误信息。 我想在运行此应用程序时执行此操作,它会显示随机数字的按钮和图像。 当我按下okbutton然后随机数将生成,它将显示该声明。 我怎么能帮到我。
答案 0 :(得分:0)
好吧很酷......
将整个随机+开关复制到onClickListeners&删除while循环: 通过这种方式,应用程序首先加载随机值,点击后它会再次执行:)
@Override
public void onClick(View arg0) {
int number;
Random random = new Random();
number = random.nextInt(2) + 1;
switch (number) {
case 1:
imageView.setBackgroundResource(R.drawable.image_2);
button1.setText("Pavel");
button2.setText("robin");
break;
case 2:
imageView.setBackgroundResource(R.drawable.image_3);
button1.setText("Galib");
button2.setText("Rimon");
break;
}
if (okbutton.isPressed()) {
number = random.nextInt(2) + 1;
}
}
}