【问题标题】:how can i get the Hour only from textClock我怎样才能只从 textClock 获得小时
【发布时间】:2020-10-07 06:00:57
【问题描述】:

在此我试图从 textclock 中仅获取一个小时并显示在我的小部件上,它在 Android 设备上完美运行,当在 BlackBerry 上进行测试时,它显示完整的时间,例如下午 12:50 而不是小时

              <TextClock
                android:id="@+id/tvHourWidget"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-black"
                android:format12Hour="hh"
                android:text="00"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/basic_twice" />

这是我尝试过的代码

【问题讨论】:

  • java代码在哪里?
  • 没有我用过的java代码,我认为文本时钟不需要java代码
  • 所以只是android:format12Hour="hh" 属性会导致不同设备上的不同行为?
  • 有些情况下时钟是 24 小时制。尝试添加android:format24Hour="hh" 属性。
  • 是的,它会导致设备上的不同行为

标签: java android xml textview


【解决方案1】:

您不能设置设备是 12 小时制还是 24 小时制,也不能在 TextView 中设置。因此,您必须同时使用 format12Hour 和 format24Hour 属性以确保在这两种情况下,格式均为 hh:

<TextClock
            android:id="@+id/tvHourWidget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-black"
            android:format12Hour="hh"
            android:format24Hour="hh"
            android:text="00"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/basic_twice" />

【讨论】:

    【解决方案2】:

    您可以使用 textview 、timer 和 simpledateformater 代替并设置所需的 DATETIME 格式

    Timer tmm=new Timer();
    public void start(TextView tv1){
    
    
        tmm.schedule(new TimerTask() {
            @Override
            public void run() {
                tv1.setText(""+ my_time("HH"));
            }
        },1000,1000);
    
    }
    public void Stop()
    {
        tmm.cancel();
    }
    
    
            public static String my_time(String format)
            {
                try{
                    return new SimpleDateFormat(format).format(Calendar.getInstance().getTime());
    
    
                }catch (Exception ex){}
                return null;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      相关资源
      最近更新 更多