2017-02-28 11 views
-1

私のアプリは乱数を生成してTextViewに設定し、同時にカウントダウンタイマーからTextViewに1つのボタンをクリックして時間を設定する必要があります。しかし、私はそれを動作させる方法の解決策を見つけることができません。1つのボタンが2つのtextViewに応答する

public class GenerateToken extends ActionBarActivity { 

TextView show; 
CountDownT timer; 

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

    show =(TextView)findViewById(R.id.textView2); 
    show.setText("60"); 
} 

public void generate (View v) 
{ 
    Random rand = new Random(); 
    int number = rand.nextInt(1999999999 - 1000000000)+1000000000; 
    TextView myText = (TextView)findViewById(R.id.textView); 
    String myString = String.valueOf(number); 
    myText.setText(myString); 
     timer.start(); 
    } 

} 
public class CountDownT extends CountDownTimer{ 

    public CountDownT (long InMillisSeconds, long TimeGap){ 
     super(InMillisSeconds,TimeGap); 
    } 

    @Override 
    public void onTick(long l){ 
     show.setText((l/1000) +""); 
    } 

    @Override 
    public void onFinish(){ 
     show.setText("Request New Token.."); 
    } 
} 
+2

ようこそ。あなたの質問はほぼ大丈夫ですが、私は何が間違っているのか理解するのに困っています。現在の行動は何ですか?それはあなたの期待される行動とどのように違いますか?また、あなたの投稿を編集して、そのコードの書式設定を修正することができればそれは素晴らしいでしょう。 – Michael

答えて

0

非常に簡単です。 ButtonsetOnClickListener()と設定して、TextViewを変更してください。同様 -

button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        textView1.setText("Text changed on TextView 1"); 
        textView2.setText("Text changed on TextView2"); 
       } 
      }); 
0

これを試してください。私はあなたのボタンxmlにonClick="generate"を追加すると思います。

public class GenerateToken extends ActionBarActivity { 

TextView show; 
TextView myText; 
CountDownT timer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_generate_token); 
    myText = (TextView)findViewById(R.id.textView); 
    show =(TextView)findViewById(R.id.textView2); 
    show.setText("60"); 
} 

public void generate (View v) 
{ 
    Random rand = new Random(); 
    int number = rand.nextInt(1999999999 - 1000000000)+1000000000; 
    String myString = String.valueOf(number); 
    myText.setText(myString); 

    new CountDownTimer(30000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      show.setText(millisUntilFinished/1000+""); 
     } 

     public void onFinish() { 
      //{code to do after the timer finishes} 
     } 

    }.start(); 

} 
} 

ここで、30000はカウントするミリ秒であり、1000は間隔です。ここで30秒間カウントダウンします。あなたの必要に応じて時間を変更してください。お役に立てれば。

+0

ありがとうございます。それは私の問題を解決する。ほんとうにありがとう。 – faiz

+0

喜んで助けてください。それが働いたら答えを受け入れる。 –

関連する問題