2017-01-11 9 views
-1

ボタンがランダムに点滅していて、毎回正確な番号が選択されていることを知りたいので、私がやっているプロジェクトのために最終的にそれらをArrayListに追加することができます。TextViewで生成された乱数を表示するにはどうすればよいですか?

TextView randomTextView; 
Random r = new Random(); 

private void sequenceFunction() { 
    //Change Alpha from Fully Visible to Invisible 
    final Animation animation = new AlphaAnimation(1, 0); 
    //Duration - A Second 
    animation.setDuration(1000); 
    //Animation Rate 
    animation.setInterpolator(new LinearInterpolator()); 
    animation.setStartOffset(250); 
    animation.setDuration(250); 
    //Repeat Animation 
    animation.setRepeatCount(r.nextInt(10)); 

    // Reverse animation at the end so the button will fade back in 
    animation.setRepeatMode(Animation.REVERSE); 

    //Button 1 Flashes 
    final Button btn = (Button) findViewById(R.id.button); 
    btn.startAnimation(animation); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View view) { 
      view.clearAnimation(); 
     } 
    }); 
} 

私が今までに乱数がrandomTextView TextViewを介して生成された内容の結果を表示したいです。この部分は、ランダム関数が想定されているとおりに動作していることを知るために重要です。私はすでに試しました

randomTextView.setText(r.nextInt(10)); 

しかし、それはそれが好きではありませんでした。選択された乱数を取得する方法に関するアイデアは非常に高く評価されますか?

+0

'randomTextView.setText( "" + r.nextInt(10));あなたはrandomTextView.setText(r.nextInt'については好きではないものを ' –

+0

(10 )) '? – TWL

+0

@TWL 'setText()'には、CharSequenceまたはリソースIDが必要です。それは明らかにそうではありません。 –

答えて

1

ホープ、このことができます -

private void sequenceFunction() { 
    //Change Alpha from Fully Visible to Invisible 
    final Animation animation = new AlphaAnimation(1, 0); 
    //Duration - A Second 
    animation.setDuration(1000); 
    //Animation Rate 
    animation.setInterpolator(new LinearInterpolator()); 
    animation.setStartOffset(250); 
    //Repeat Animation 
    int randomValue = r.nextInt(); 
    // code to add value to array 
    animation.setRepeatCount(randomValue); 
    randomTextView.setText(String.valueOf(randomValue)); 

    // Reverse animation at the end so the button will fade back in 
    animation.setRepeatMode(Animation.REVERSE); 

    //Button 1 Flashes 
    final Button btn = (Button) findViewById(R.id.button); 
    btn.startAnimation(animation); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View view) { 
      view.clearAnimation(); 
     } 
    }); 


} 
0

あなたがTextViewのドキュメントの外観を持っている場合、あなたはsetText(int)が実際にリソースID期待していることがわかります:あなたは、単に

randomTextView.setText("" + r.nextInt(10)); 
を行うことができ、 https://developer.android.com/reference/android/widget/TextView.html#setText(int)

だからあなたがたCharSequenceにあなたのint型を有効にする必要がありますが

は整数を文字列に変換します。

+0

こんにちは、お返事ありがとうございます。私はanimation.setRepeatCount(r.nextInt(10))の番号を取得します。私がrandomTextView.setText(r.nextInt(10)+ "")をしたときのように。それは新しい乱数であり、アニメーションの点滅を生成するために使用しているものではありません。 –

+0

は、この場合には、単にこのような変数に値を格納: 'INT乱数= r.nextInt(10); '' animation.setRepeatCount(乱数); '' ... '' randomTextView。 setText(Integer.toString(random)); ' – SimMac

+0

いくつかの変数に' r.nextint(10) 'を保存し、それを' animation.setRepeatCount() 'に渡してどこでも使用できます。 –

0

TextView.setTextは、resource ID(整数)またはCharSequence(たとえば、String)のいずれかで使用できます。

ない使用"" + r.nextInt(10)を行い

int random = r.nextInt(10); 
randomTextView.setText(String.valueOf(random); 
randomTextView.setText(Integer.toString(random); 
randomTextView.setText(String.format("%d", random); 

含めて、これを行うには多くの方法があります。私は知っている、それは短い、それは便利ですが、それは単に非効率的で悪いスタイルです。

+0

こんにちは、私はアニメーション.setRepeatCount(r.nextInt(10))によって生成されている正確な乱数を取得するためにこれを使用すると思います。 ? –

+0

このコードは生成された数値を 'TextView'に表示します。数値を複数回使用する場合は、次のような変数にキャッシュする必要があります。 'int number = r.nextInt(10); randomTextView.setText(String.valueOf(number)); '変数' number'を使用します。 –

関連する問題