Android计时码表:时间格式问题 [英] Android Chronometer: time format issue

查看:190
本文介绍了Android计时码表:时间格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的天文钟表告诉我后不停?

Why my chronometer wouldn't stop after I've told it to?

我发现它似乎与我在7英寸Galaxy Tab上测试过的平板电脑隔离.我不知道为什么它可以在我的手机上运行而不能在平板电脑上运行. Chronometer在两种布局中的设置都相同(因为我为不同的屏幕尺寸使用了不同的布局).

I have found out that it seems to be isolated to the tablet I have tested it on (7 inch Galaxy Tab). I have no idea why it would work on my phone yet not work on the tablet. The Chronometer is set up the same in both layouts (since I am using different layouts for the different screen sizes).

这是在类中处理Chronometer的方式:

Here is how the Chronometer is handled in the class:

this.chrono = (Chronometer) findViewById(R.id.calling_crono);

chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

            public void onChronometerTick(Chronometer chronometer) {
                String currentTime= chrono.getText().toString();
            if(currentTime.equals("00:20")) 
            {
              chrono.stop();
              Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
            Bundle b = new Bundle();

            int end = -5;
            b.putInt("score", score);
            b.putInt("end", end);
            intent.putExtras(b); 
            startActivity(intent);
            finish();
             }
            }
        });
        public void startCrono() {
 chrono.setBase(SystemClock.elapsedRealtime());
 chrono.start();
}

以下是在普通尺寸屏幕中的设置方法:

Here is how it is set up in the normal-size screen:

<Chronometer
    android:id="@+id/calling_crono"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:textColor="#000099" 
    android:textStyle="bold"
    android:textSize="20sp"/>

这是在大尺寸屏幕中:

 <Chronometer
    android:id="@+id/calling_crono"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="50sp"
    android:textColor="#000099"
    android:textStyle="bold" />

基本问题是手机上的手机正常工作,即20秒钟后继续开机,但在平板电脑上,手机却不会停止并继续前进.是否有某些原因可以在我的手机(Xperia)上而不是在平板电脑上运行?我唯一能想到的就是它们使用不同的xml布局文件的事实.

The basic problem is that on the phone it works as normal, which is to move on after 20 seconds, but on the tablet it won't stop and keeps going ahead. Is there some reason why this works on my phone (Xperia), but not on the tablet. The only thing I can think off is the fact that they use different xml layout files.

推荐答案

我相信您遇到的问题与不同布局xml文件" 没有关系.它似乎是一个Android 错误,可能是这样的:

I believe what you're experiencing isn't related to "different layout xml files". It seems to be an Android bug, probably this one: Chronometer in Android 4.2.

要检查我是否在普通屏幕尺寸的手机上( Android 4.2.2 )和三星进行了几次测试(只有一个布局) Galaxy Tab 7 ( Android 4.0.3 ).我得到返回值chrono.getText().toString()

To check that I've done a couple of tests (with only one layout) on a normal screen size phone (Android 4.2.2) and Samsung Galaxy Tab 7 (Android 4.0.3). I get the following string format of the returned value of chrono.getText().toString()

  1. Android 4.0.3: "mm:ss"
  2. Android 4.2.2: "m:ss"
  1. Android 4.0.3: "mm:ss"
  2. Android 4.2.2: "m:ss"

对应于报告的 bug .

假设您的Galaxy Tab 7 具有 Android 4.2 ,解释了为什么计时器不停止并继续前进的原因: c13>是false,因为您将两个不同格式的字符串 "m:ss""mm:ss"进行了比较.

Assuming that your Galaxy Tab 7 has Android 4.2 explains why the Chronometer doesn't stop and keeps going ahead: the condition currentTime.equals("00:20") is false because you compare two strings of different format, "m:ss" with "mm:ss".

如果我的假设是错误的,并且与Android版本无关,那么有解决方案.

If my assumption is wrong and it does not regard to the Android version yet there is a solution to handle it.

解决方案:我建议以某种方式设置currentTime的格式,例如"mm:ss",然后将格式化的时间与"00:20"进行比较.以下代码段使用 SimpleDateFormat :

Solution: I recommend formatting currentTime in a certain manner, e.g. "mm:ss" and then compare the formatted time with "00:20". The following code snippet uses SimpleDateFormat:

chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

        public void onChronometerTick(Chronometer chronometer) {

            String currentTime= chrono.getText().toString();
            Date date = null;
            try {
                date = new SimpleDateFormat("mm:ss").parse(currentTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String newTime = new SimpleDateFormat("mm:ss").format(date);
            ...

现在使用newTime进行比较:

if(newTime.equals("00:20")) {
    // your code
}

这篇关于Android计时码表:时间格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆