我想在
TextView
中以09:41时分格式展示当前时间,并持续更新,有可能实现吗?我尝试过一些方法,但并没有得到我想要的结果。Android已经有一个视图可以解决这个问题。
http://developer.android.com/reference/android/widget/TextClock.html
您可以直接在XML中使用它,如下所示:
<TextClock
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
最低API版本为17,如果您需要使用比这更低的版本,只需查看http://developer.android.com/reference/android/widget/DigitalClock.html
最坏情况下,您可以子类化TextView并从TextClock源代码中窃取代码。这应该是相当简单的。
这样做应该可以解决问题。
final Handler someHandler = new Handler(getMainLooper());
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
someHandler.postDelayed(this, 1000);
}
}, 10);
当Activity
暂停时,您应该保留对处理程序和可运行对象的引用以便在需要时取消它,在onDestroy
中确保删除所有对处理程序的回调并将其设置为null
,而在恢复后则要重新开始。
boolean run=true; //set it to false if you want to stop the timer
Handler mHandler = new Handler();
public void timer() {
new Thread(new Runnable() {
@Override
public void run() {
while (run) {
try {
Thread.sleep(20000);
mHandler.post(new Runnable() {
@Override
public void run() {
Calendar c = Calendar.getInstance();
int min = c.get(Calendar.MINUTE);
int hour=c.get(Calendar.HOUR);
TextView.setText(String.valueOf(hour)+":"+String.valueOf(min));
}
});
} catch (Exception e) {
}
}
}
}).start();}
timer();
setContentView
之前调用 timer()
吗? - Ajinkya More如果需要直接使用自定义日期和时间:
android:format12Hour="hh:mm:ss a"
android:format24Hour="MMM dd, yyyy"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_screenlock"
app:layout_constraintStart_toStartOf="parent">
<TextClock
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:textColor="@color/_white"
android:textSize="70sp"
android:format12Hour="hh:mm:ss a"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextClock
android:id="@+id/textDateTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/_white"
android:textSize="28sp"
android:format24Hour="MMM dd, yyyy"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
[![date_time_update_directly][1]][1]
[1]: https://istack.dev59.com/JZWUj.webp
为什么不使用Java的计时器和任务?它们非常易用、可靠且易于理解。
以下是一个示例:
public class ClockCounter extends TimerTask{
private long time = System.currentTimeMillis();
@Override
public void run(){
time += 1000; //add 1 second to the time
//convert ms time to viewable time and set MainActivity.text (textview) text to this.
}
public long getTime(){ return time; }
}
public class MainActiviy extends Activity{
public static TextView text;
private Timer timer;
@Override
public void onCreate(){
//default code
//set text view according to id
timer = new Timer();
timer.schedule(new ClockCounter(), 0, 1000); //schedule clock counter to repeat every 1 seconds (1000 ms)
}
}
我知道这里有一些要做的笔记,但任何有Java合理经验的安卓开发者都应该能够弄清楚如何完成这些事情。
希望这可以帮到你!
编辑 要将日期格式化为可视字符串,您可以使用以下代码(仅使用Java的已安装API)
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm"); //Hourse:Minutes
String dateFormatted = formatter.format(date); //string representation
要获取时间和日期,您可以使用 Calendar 类。
Calendar cal = Calendar.getInstance();
String time = ""+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);