应用程序将分钟读取为秒
Application read minutes as seconds
我是新来的,如果您有什么不明白的地方,请见谅。
因此,当我通过 + 和 - 按钮设置值时,我正在制作倒数计时器,我想将其设置为“1min:10seconds”形式,但我的应用程序将其读取为 110 秒。如何转换?
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3;
TextView tv1;
int czasrundy=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.button1);
btn2=findViewById(R.id.button6);
btn3=findViewById(R.id.button7);
tv1=findViewById(R.id.textView20);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String czarundy=tv1.getText().toString();
final long dłrundy = getTimeInLong(czarundy) * 1000;
CountDownTimer countDownTimer=new CountDownTimer(dłrundy,1000) {
@Override
public void onTick(long millisUntilFinished) {
tv1.setText ("" +millisUntilFinished/1000);
}
@Override
public void onFinish() {
}
}.start();
}
});
}
public void odejmowanie1(View view) {
if (czasrundy>=5){
czasrundy=czasrundy-5;
display2(czasrundy);
}
}
private void display2(int czasrundy) {
TextView displayInteger =(TextView)findViewById(R.id.textView20);
String formatowanczas=String.format("%d:%02d", czasrundy/60, czasrundy %60);
displayInteger.setText(formatowanczas);
}
public void dodawanie1(View view) {
czasrundy=czasrundy+5;
display2(czasrundy);
}
public long getTimeInLong(String input) {
StringBuilder builder = new StringBuilder();
String[] splittedString = input.split(":");
builder.append(splittedString[0]);
builder.append(splittedString[1]);
return Long.parseLong(builder.toString());
}
}
我认为您的问题是,当您解析字符串时,您在 stringbuilder 中连接了“1”和“10”。你应该解析做这样的事情
public long getTimeInLong(String input) {
StringBuilder builder = new StringBuilder();
String[] splittedString = input.split(":");
Long min = Long.parseLong(splittedString[0]) * 60;
Long sec = Long.parseLong(splittedString[1]);
return min + sec;
}
我是新来的,如果您有什么不明白的地方,请见谅。 因此,当我通过 + 和 - 按钮设置值时,我正在制作倒数计时器,我想将其设置为“1min:10seconds”形式,但我的应用程序将其读取为 110 秒。如何转换?
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3;
TextView tv1;
int czasrundy=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.button1);
btn2=findViewById(R.id.button6);
btn3=findViewById(R.id.button7);
tv1=findViewById(R.id.textView20);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String czarundy=tv1.getText().toString();
final long dłrundy = getTimeInLong(czarundy) * 1000;
CountDownTimer countDownTimer=new CountDownTimer(dłrundy,1000) {
@Override
public void onTick(long millisUntilFinished) {
tv1.setText ("" +millisUntilFinished/1000);
}
@Override
public void onFinish() {
}
}.start();
}
});
}
public void odejmowanie1(View view) {
if (czasrundy>=5){
czasrundy=czasrundy-5;
display2(czasrundy);
}
}
private void display2(int czasrundy) {
TextView displayInteger =(TextView)findViewById(R.id.textView20);
String formatowanczas=String.format("%d:%02d", czasrundy/60, czasrundy %60);
displayInteger.setText(formatowanczas);
}
public void dodawanie1(View view) {
czasrundy=czasrundy+5;
display2(czasrundy);
}
public long getTimeInLong(String input) {
StringBuilder builder = new StringBuilder();
String[] splittedString = input.split(":");
builder.append(splittedString[0]);
builder.append(splittedString[1]);
return Long.parseLong(builder.toString());
}
}
我认为您的问题是,当您解析字符串时,您在 stringbuilder 中连接了“1”和“10”。你应该解析做这样的事情
public long getTimeInLong(String input) {
StringBuilder builder = new StringBuilder();
String[] splittedString = input.split(":");
Long min = Long.parseLong(splittedString[0]) * 60;
Long sec = Long.parseLong(splittedString[1]);
return min + sec;
}