显示一个随机图像单击按钮时

显示一个随机图像单击按钮时

本文介绍了显示一个随机图像单击按钮时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更早,只是得到了更多的困惑,所以我会尝试更多precise。我想提出一个应用程序中,我有7一副扑克牌。我想点击在甲板上,并有7个卡之一弹出屏幕上。到目前为止,我有

I tried earlier and just got more confused so i will try and be more precise. I am making an app in which i have a deck of 7 cards. I want to click on the deck and have one of the 7 cards pop up on the screen. So far I have

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final MediaPlayer mpClick = MediaPlayer.create(this, R.raw.click);
        randomM = (EditText) findViewById(R.id.randomM);

        //button 1 start
        Button bMythos = (Button) findViewById(R.id.mythos);
        bMythos.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpClick.start();
                Random r = new Random();
                int n=r.nextInt(7) + 1;
                randomM.setText(String.valueOf(n));
            }
        });
        //button 1 end
    }

}

到目前为止,这显示卡甲板我点击并产生一个随机数(文本框几乎是我知道的随机数生成器的工作,将被删除时,我找出显示器)。

So far this displays the card deck which i click on and a random number is generated (the text box is nearly for me to know the random number generator is working; will be removed when i figure out the display).

所以我的问题我怎样才能获得随机数对应一个随机卡并获得显示卡? - 卡被标记mythos1,mythos2,等等,所以我认为我可以做一些与神话(将String.valueOf(N)),但没有工作(除非我正在做别的错误),如果你不能告诉我不知道我在做什么]

So my questionHow can i get the random number to correspond with a random card and get the card displayed? - the cards are labeled mythos1, mythos2, etc so i assumed i could do something with mythos(String.valueOf(n)) but that didn't work (unless i'm doing something else wrong) [if you can't tell i have no idea what i'm doing]

推荐答案

试试这个方法

 int[] cards={R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7};
 Random r = new Random();
 int n=r.nextInt(7);
 imageview.setImageResource(cards[n]);

这篇关于显示一个随机图像单击按钮时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:56