gpt4 book ai didi

java - 从可更改的 TextView 获取整数以在倒计时器中使用

转载 作者:行者123 更新时间:2023-12-01 12:38:58 25 4
gpt4 key购买 nike

我有一个带有计时器的 Android 应用程序。默认情况下,显示为 00:00:00(小时、分钟、秒)。用户可以按 H M S 并围绕圆圈拖动 slider 来更改小时、分钟或秒,具体取决于单击的哪一个。我试图通过解析可更改的 TextView 中的小时、分钟和秒的数字来创建一个 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>

为了您的方便,我有整个类(class)的粘贴链接。

http://pastebin.com/kbb1gpGe

如果需要任何说明,请告诉我。谢谢!

最佳答案

您仅在 onCreate() 方法中解析 hourintminuteintsecondint 一次。

将其全部移至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) {

...
}

关于java - 从可更改的 TextView 获取整数以在倒计时器中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com