2016-11-26 3 views
0

こんにちは皆さん、私はこの1頭で私の頭を傷つけていて、それは簡単な問題だと確信しています。ボタンのクリック時にランダムな整数を生成するアクティビティをリフレッシュしないでクリック

私は、別のボタンが押されると、ボタンのグループにランダムな文字のテキストを生成するプログラムを持っています。 アプリケーションを開くときに正常に読み込まれ、最初にボタンが押されると正しく生成されますが、その後はランダムな文字を再生成することができません。

私はインテントを追加して、基本的なアクティビティを本質的に「リフレッシュ」(言い回しがない)にすることができますが、ボタンクリックのカウンタを追加したいときはアクティビティがリセットされます。

{Intent intent = new Intent(MainActivity.this,  MainActivity.class); 
    startActivity(intent); 
     finish(); 
     overridePendingTransition(0, 0); 

私はそれ以外の方法で私の心を包んでいるようです。ボタンをクリックするたびにJavaを再実行する必要があるようです。助言がありますか?

ここでコードはいくつかありますが、私はa)簡単な背景を設定したかったのでb)は後でクリック可能にするため、テキストビューではなくボタンを使用しました。事前にありがとう!

public class MainActivity extends Activity 
{ 

    Button spin; 
    Button reel1,reel2,reel3,reel4; 
    private String rnd,rnd2,rnd3,rnd4; 

    int count1=50; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    {super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     addListenerOnButton();} 


    private static String rndm[] = {"A","B","C","D"}; 
    {rnd = rndm[(int) (Math.random() * rndm.length)];} 
    private static String rndm2[] = {"A","B","C","D"}; 
    {rnd2 = rndm2[(int) (Math.random() * rndm2.length)];} 
    private static String rndm3[] = {"A","B","C","D"}; 
    {rnd3 = rndm3[(int) (Math.random() * rndm3.length)];} 
    private static String rndm4[] = {"A","B","C","D"}; 
    {rnd4 = rndm4[(int) (Math.random() * rndm4.length)];} 

    public void perform_action(View v){} 

    public void addListenerOnButton() { 


     spin = (Button) findViewById(R.id.spin); 
     spin.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        {Toast.makeText(getApplicationContext(),"button pressed",  Toast.LENGTH_SHORT).show();} 


       {Button tv = (Button)  findViewById(R.id.reel1); 
        tv.setText(String.valueOf(rnd)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel2); 
        tv.setText(String.valueOf(rnd2)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel3); 
        tv.setText(String.valueOf(rnd3)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel4); 
        tv.setText(String.valueOf(rnd4)); 
        tv.setTextColor(Color.parseColor("#000000")); 

答えて

1

私はそれを理解しました!うわー。しかし、私は正しい答えのために自分自身をアップアップすることはできません。

public class MainActivity extends Activity 
{ 

    Button spin; 
    Button reel1,reel2,reel3,reel4; 
    private String rnd,rnd2,rnd3,rnd4; 

    int count1=50; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    {super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     addListenerOnButton();} 

    public void perform_action(View v){} 

    public void addListenerOnButton() { 


     spin = (Button) findViewById(R.id.spin); 
     spin.setOnClickListener(new OnClickListener() { 

       @Override 
      public void onClick(View v) { 



       String rndm[] = {"A","B","C","D"}; 
       {rnd = rndm[(int) (Math.random() *  rndm.length)];} 
       String rndm2[] = {"A","B","C","D"}; 
       {rnd2 = rndm2[(int) (Math.random() *  rndm2.length)];} 
       String rndm3[] = {"A","B","C","D"}; 
       {rnd3 = rndm3[(int) (Math.random() *  rndm3.length)];} 
       String rndm4[] = {"A","B","C","D"}; 
       {rnd4 = rndm4[(int) (Math.random() *  rndm4.length)];} 


        {Toast.makeText(getApplicationContext(),"button pressed",  Toast.LENGTH_SHORT).show();} 


       {Button tv = (Button)  findViewById(R.id.reel1); 
        tv.setText(String.valueOf(rnd)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel2); 
        tv.setText(String.valueOf(rnd2)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel3); 
        tv.setText(String.valueOf(rnd3)); 
        tv.setTextColor(Color.parseColor("#000000"));} 

       {Button tv = (Button)  findViewById(R.id.reel4); 
        tv.setText(String.valueOf(rnd4)); 
        tv.setTextColor(Color.parseColor("#000000")); 

ランダムストリングを自分のOnclickイベントに移動し、プライベートおよび静的パーツを削除しました。それからwhala!物事は魅力のように機能します!

+2

あなた自身の答えを受け入れることができます。それをアップヴォートする必要はありません。 – Acapulco

関連する問題