如何在android中的textview上随机生成24个字符串值?

时间:2014-03-29 02:40:51

标签: java android random

我想在24 textview上生成24个字符串值,我在android中创建了xml文件,我也为getid和set listener编写代码,但这里没有提到。 在覆盖oncick方法中,我定义了click fun方法。 在这段代码中我只是编码整数值,但现在我想比较具有静态值的字符串数组 String [] value = {11,12,13,14,15,16,17,18,19,20,21,22,11,12,13,14,15,16,17,18,19,20, 21,22}; 请帮我解决我的问题。

public class GameDemo extends Activity implements AnimationListener, OnClickListener
{
    FrameLayout iv1,iv2,iv3,iv4,iv5,iv6,iv7,iv8,iv9,iv10,iv11,iv12,iv13,iv14,iv15,iv16,iv17,iv18,iv19,iv20,iv21,iv22,iv23,iv24;
    TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10,tv11,tv12,tv13,tv14,tv15,tv16,tv17,tv18,tv19,tv20,tv21,tv22,tv23,tv24;
    Animation an1,an2;
    int i1,counter,score,rev_count,level_counter;
    boolean r_c,s_c;
    Integer[] no;
    FrameLayout[] count,rem,revise; 
    final Context context = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_demo);
    an1 = AnimationUtils.loadAnimation(this,R.anim.flip1);
    an1.setAnimationListener(this);
    an2 = AnimationUtils.loadAnimation(this,R.anim.flip1);
    an2.setAnimationListener(this);
    level_counter=24;
    getIDForAll();
    setListnerForAll();
    counter=1;
    rev_count=0;
    no = new Integer[25];
            count = new FrameLayout[3];
    rem = new FrameLayout[3];
    revise = new FrameLayout[8];
    int i=0;
    for(i=0 ; i<24 ; i++)
    {
        Random r = new Random();
        i1 = r.nextInt(25 - 1) + 1;
        if(i!=0)
        {
            while(Arrays.asList(no).contains(i1))
            {
                r = new Random();
                i1 = r.nextInt(25 - 1) + 1;
            }
            no[i]= i1 ;
        }
        else
        {
            no[i]= i1 ;
        }
    }

public void click_fun(FrameLayout arg0, TextView arg1, int n)
{
if(counter<2)
{
    arg0.setAnimation(an1);
    if(no[n]<=12)
    {
        arg1.setText(""+no[n]);
        arg0.setBackgroundResource(android.R.color.white);
    }
    else
    {
        arg1.setText(""+(no[n]-12));
        arg0.setBackgroundResource(android.R.color.white);
    }
    arg0.setTag(no[n]);
    count[counter] = arg0;
    counter++;  
    Log.i("animate", "anim2");
}
else
{
    Log.i("animate", "animo1");
    arg0.setAnimation(an1);
    if(no[n]<=12)
    {
        arg1.setText(""+no[n]);
        arg0.setBackgroundResource(android.R.color.white);
    }
    else
    {
        arg1.setText(""+(no[n]-12));
        arg0.setBackgroundResource(android.R.color.white);
    }
    arg0.setTag(no[n]);
    count[counter] = arg0;
    counter++;
    Log.i("animate", "animo2");
    Uri uri = Uri.parse("android.resource://com.game/drawable/ic_launcher");
    int temp1 = (Integer) count[1].getTag();
    int temp2 = (Integer) arg0.getTag();
    Log.i("animate", "animo3");
    if((temp1-temp2)==-12 || temp1-temp2==12)
    {
        rem[1]=count[1];
        rem[2]=count[2];
        s_c=true;
        score=score+10;
        score_lbl.setText("Score : "+score);
        score=score+10;
        score_lbl.setText("Score : "+score);
        level_counter=level_counter-2;
    }   
    else
    {
        rev_count++;
        revise[rev_count]=count[1];
        rev_count++;
        revise[rev_count]=count[2];
        r_c=true;
        score=score-2;
        score_lbl.setText("Score : "+score);
    }
    counter=1;
}

}

1 个答案:

答案 0 :(得分:0)

我认为我完全理解你的处境。 1.创建一个String资源数组:

private Integer[] Strings = { many, strings, R.string.stringa };

2。调用方法来获取随机资源:

getRandomString(int);

3。返回一个随机字符串:

private String getRandomString(int random)
    return getString( Strings[ random ] );