2016-05-04 18 views
0

こんにちは私はタイマーカウンターを作りたいと思うし、タイマーをまだカウントし、ボタンをクリックすると一時停止または停止したいが、今私はアプリでタイマーを実行することができますが、私はタイマーの値でフラグメント上のtextViewを設定することができませんアプリケーションでTextViewの値を設定する方法は?

テキストをテキスト表示に設定するにはどうすればよいですか?それはタイマーになるでしょうか?

ここに私のコード:

public class TimerFragment extends BaseFragment { 
    private ImageView btnPause, btnStop, iconCreditInfo, iconStockOpname, iconOrder, iconReturn; 
    private TextView timerValue, txtCreditInfo, txtStockOpname, txtOrder, txtReturn, namaClient, keterangan, dateValue; 
    private long startTime = 0L; 
    private Handler customHandler = new Handler(); 
    long timeInMilliseconds = 0L; 
    long timeSwapBuff = 0L; 
    long updatedTime = 0L; 
    private float textSizeBig, imgSizeBig, textSizeSmall, imgSizeSmall; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.fragment_timer, container, false); 
     initialize(); 
     //start-time 
     /* 
     startTime = SystemClock.uptimeMillis(); 
     customHandler.postDelayed(updateTimerThread, 0);*/ 
     btnPause.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // App.appInstance.startTimer(); 
       /*App app = ((App) getActivity().getApplicationContext()); 
       app.afficher(); 
       timerValue.setText(app.getTimer().toString());*/ 
      } 
     }); 

     //make default icon and text size 
     creditInfoDefault(); 
     orderDefault(); 
     stockOpnameDefault(); 
     returnDefault(); 

     //set today date 
     DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
     Date date = new Date(); 
     dateValue.setText(dateFormat.format(date).toString()); 


     return rootView; 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
    // cancel(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    private Runnable updateTimerThread = new Runnable() { 

     public void run() { 

      timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

      updatedTime = timeSwapBuff + timeInMilliseconds; 

      int secs = (int) (updatedTime/1000); 
      int mins = secs/60; 
      secs = secs % 60; 

      timerValue.setText("" + mins + ":" 
        + String.format("%02d", secs)); 
      customHandler.postDelayed(this, 0); 
     } 

    }; 


} 

ここでアプリのコード:

public class App extends Application { 

    public static App appInstance; 
    private SimpleDateFormat dateFormat; 
    private long startTime = 0L; 
    private Handler customHandler = new Handler(); 
    long timeInMilliseconds = 0L; 
    long timeSwapBuff = 0L; 
    long updatedTime = 0L; 
    private StringBuilder timer; 

    public StringBuilder getTimer() { 
     return timer; 
    } 

    public void setTimer(StringBuilder timer) { 
     this.timer = timer; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     appInstance = this; 

     dateFormat = new SimpleDateFormat("mm:ss"); 
    } 

    public void afficher() { 
     timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

     updatedTime = timeSwapBuff + timeInMilliseconds; 

     int secs = (int) (updatedTime/1000); 
     int mins = secs/60; 
     secs = secs % 60; 

     timer.append(""); 
     timer.append(mins); 
     timer.append(":"); 
     timer.append(String.format("%02d", secs)); 
     // Toast.makeText(getBaseContext(),mins).show(); 
      /* timerValue.setText("" + mins + ":" 
        + String.format("%02d", secs));*/ 
     customHandler.postDelayed((Runnable) this, 0); 
     /* 
     handler.postDelayed(runnable,1000);*/ 
    } 

    public void startTimer() { 
     runnable.run(); 
    } 

    public void stopTimer() { 
     handler.removeCallbacks(runnable); 
    } 

    Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 

     public void run() { 

     } 
    }; 

} 
+1

クロノメーターにチェックを入れましたか? http://developer.android.com/reference/android/widget/Chronometer.htmlこれはあなたがやっていることに役立つかもしれません。 –

+1

TextViewを静的にして、TimerFragment.timerValueを通してそれにアクセスできます。しかし、断片が目に見えるようにする必要があります。 –

+0

テキストを設定する場所は?それを正しく指定できますか?あなたの質問は不完全で曖昧です –

答えて

0

あなたはrunOnUiThreadを使用することができます。
私はtimerValueがあなたのtextViewだと仮定しています。あなたのスレッド内 :

your_activity.runOnUiThread(new Runnable() { 
    public void run() { 
     timerValue.setText("" + mins + ":"+String.format("%02d",secs)); 
    } 
}); 

それとも、クロノウィジェットを使用することができます。このtutorialをチェックしてください。

関連する問題