如何在应用程序中设置TextView的值?

时间:2016-05-04 09:17:34

标签: android timer

嗨我想制作一个计时器计数器,我希望计时器仍在计数,暂停或停止当我点击一个按钮但现在我可以在应用程序中运行计时器,但我无法在片段上设置带有计时器值的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 个答案:

答案 0 :(得分:0)

您可以使用runOnUiThread 我假设timerValue是你的textView。 在线程内:

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

或者您可以使用 Chronometer 小部件。看看这个tutorial