从可更改的TextView获取内容以在CountDown Timer中使用

时间:2014-08-14 06:22:58

标签: java android xml timer countdowntimer

我有一个带有计时器的Android应用程序。默认情况下,它显示为00:00:00(小时,分钟,秒)。用户可以按下H M S并围绕圆圈拖动洗涤器以更改小时,分钟或秒,具体取决于单击哪一个。我试图通过从可更改的TextViews中解析小时,分钟和秒的数字来创建一个CountDownTimer,将其全部转换为秒,然后启动计时器;但是,它不会启动,因为总数为零。

这是我的屏幕,显示分钟数。

enter image description here

我不是真的,为什么我无法从三个textview中获取int值。

我做过研究,但没有太多类似于这个问题。

这是我的代码中的setActionListener方法:

private void setActionListeners() {
number_text = (TextView) findViewById(R.id.hour_progress_number);
minute_text = (TextView) findViewById(R.id.minute_progress_number);
second_text = (TextView) findViewById(R.id.second_progress_number);

 hourint = Integer.valueOf(number_text.getText().toString());

 minuteint = Integer.valueOf(minute_text.getText().toString());

 secondint = Integer.valueOf(second_text.getText().toString());

Log.i("YourActivity", "Hours: " + hourint);

Log.i("YourActivity", "Minutes: " + minuteint);

Log.i("YourActivity", "Seconds: " + secondint);



totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = totalTimeCountInMilliseconds/1000;


start_timer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

       // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);

        countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
            // 500 means, onTick function will be called at every 500 milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;
                mSeekArc.setVisibility(View.INVISIBLE);


                if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                   // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                    // change the style of the textview .. giving a red alert style

                    if ( blink ) {
                        number_text.setVisibility(View.VISIBLE);
                        minute_text.setVisibility(View.VISIBLE);
                        second_text.setVisibility(View.VISIBLE);


                        // if blink is true, textview will be visible
                    } else {
                        number_text.setVisibility(View.INVISIBLE);
                        minute_text.setVisibility(View.INVISIBLE);
                        second_text.setVisibility(View.INVISIBLE);


                    }

                    blink = !blink;         // toggle the value of blink
                }

                second_text.setText(String.format("%02d", seconds % 60));
                minute_text.setText(String.format("%02d", seconds / 60));
                number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
            }


            @Override
            public void onFinish() {
                // this function will be called when the timecount is finished
                //textViewShowTime.setText("Time up!");
                number_text.setVisibility(View.VISIBLE);
                minute_text.setVisibility(View.VISIBLE);
                second_text.setVisibility(View.VISIBLE);
                mSeekArc.setVisibility(View.VISIBLE);
            }

        }.start();

    }
});

我的xml文件不是很复杂,但如果需要,它是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:seekarc="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
                        >

    <FrameLayout
        android:id="@+id/seekArcContainer"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        >

    </FrameLayout>

    <include
        layout="@layout/controls"
        android:id="@+id/controls" />

    <com.triggertrap.seekarc.SeekArc
        android:id="@+id/seekArc"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center_horizontal|bottom"
        seekarc:thumb="@drawable/custom_seek_arc_control_selector"
        android:padding="30dp"
        seekarc:rotation="0"
        seekarc:startAngle="0"
        seekarc:sweepAngle="360"
        seekarc:touchInside="true"
        seekarc:arcColor="#30ff5b56"
        seekarc:progressColor="#ffff3a35"
        android:layout_below="@+id/seekArcContainer"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/hour_progress_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/red_highlight"
        android:text="00"
        android:textSize="55sp"
        android:layout_marginTop="130dp"
        android:layout_below="@+id/seekArcContainer"
        android:layout_alignLeft="@+id/hourtext"
        android:layout_alignStart="@+id/hourtext" />


    <TextView
        android:id="@+id/minute_progress_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/red_highlight"

        android:text="00"
        android:textSize="55sp"
        android:layout_alignTop="@+id/hour_progress_number"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/second_progress_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/red_highlight"

        android:text="00"
        android:textSize="55sp"
        android:layout_alignTop="@+id/minute_progress_number"
        android:layout_alignRight="@+id/secondtext"
        android:layout_alignEnd="@+id/secondtext" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="@string/Hours"
        android:textSize="55sp"
        android:textColor="@color/red_highlight"
        android:id="@+id/hourtext"
        android:layout_marginLeft="45dp"
        android:layout_alignTop="@+id/minutetext"
        android:layout_alignLeft="@+id/seekArc"
        android:layout_alignStart="@+id/seekArc" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Minutes"
        android:clickable="true"

        android:textSize="55sp"
        android:textColor="@color/red_highlight"
        android:id="@+id/minutetext"
        android:layout_below="@+id/seekArc"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Second"
        android:clickable="true"
        android:textSize="55sp"
        android:textColor="@color/red_highlight"
        android:id="@+id/secondtext"
        android:layout_alignTop="@+id/minutetext"
        android:layout_alignRight="@+id/seekArc"
        android:layout_alignEnd="@+id/seekArc"
        android:layout_marginRight="43dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="H"
        android:textColor="@color/red_highlight"
        android:id="@+id/little_hour_text2"
        android:layout_alignBottom="@+id/hour_progress_number"
        android:layout_toLeftOf="@+id/minute_progress_number"
        android:layout_toStartOf="@+id/minute_progress_number" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="M"
        android:textColor="@color/red_highlight"
        android:id="@+id/little_minute_text2"
        android:layout_toEndOf="@+id/minute_progress_number"
        android:layout_alignBottom="@+id/second_progress_number"
        android:layout_toRightOf="@+id/minute_progress_number" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="S"
        android:textColor="@color/red_highlight"
        android:id="@+id/little_second_text2"
        android:layout_toEndOf="@+id/second_progress_number"
        android:layout_alignBottom="@+id/second_progress_number"
        android:layout_toRightOf="@+id/second_progress_number" />


</RelativeLayout>

为方便起见,我有一个全班的pastebin链接。

http://pastebin.com/kbb1gpGe

如果需要澄清,请与我们联系。谢谢!

1 个答案:

答案 0 :(得分:1)

您正在使用hourint方法解析minuteintsecondintonCreate()一次。

将其全部移至start_timer onClick()

@Override
public void onClick(View view) {
    hourint = Integer.valueOf(number_text.getText().toString());

    minuteint = Integer.valueOf(minute_text.getText().toString());

    secondint = Integer.valueOf(second_text.getText().toString());

    Log.i("YourActivity", "Hours: " + hourint);

    Log.i("YourActivity", "Minutes: " + minuteint);

    Log.i("YourActivity", "Seconds: " + secondint);

    totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count for 3 minutes = 180 seconds
    timeBlinkInMilliseconds = totalTimeCountInMilliseconds/1000;

    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) { 

    ...
}