我环顾四周,到目前为止我似乎没有任何工作......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deskclock);
TextView tvTime = (TextView) findViewById(R.id.tvTime);
TextView tvDate = (TextView) findViewById(R.id.tvDate);
java.util.Date noteTS = Calendar.getInstance().getTime();
String time = "hh:mm"; // 12:00
tvTime.setText(DateFormat.format(time, noteTS));
String date = "dd MMMMM yyyy"; // 01 January 2013
tvDate.setText(DateFormat.format(date, noteTS));
我基本上希望setText方法每隔一秒左右更新或刷新一次,所以我的时钟实际上就像应该做的那样。我已经看到像处理程序和运行的方法,没有任何工作,所以任何帮助这将是非常感谢。 :)
答案 0 :(得分:100)
在onCreate()方法中添加以下代码:
Thread thread = new Thread() {
@Override
public void run() {
try {
while (!thread.isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// update TextView here!
}
});
}
} catch (InterruptedException e) {
}
}
};
thread.start();
此代码启动一个每轮休眠1000毫秒的线程。
答案 1 :(得分:18)
这是一个非常古老的问题,我确信那里有很多资源。但是,为了安全起见,传播这个词永远不会太多。目前,如果其他人想要达到OP提出的要求,您可以使用:android.widget.TextClock
。
TextClock文档here。
这是我用过的东西:
<android.widget.TextClock
android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timeZone="GMT+0000" <!--Greenwich -->
android:format24Hour="dd MMM yyyy k:mm:ss"
android:format12Hour="@null"
android:textStyle="bold"
android:layout_alignParentEnd="true" />
答案 2 :(得分:17)
扩展@endian的答案,你可以使用一个线程并调用一个方法来更新TextView。下面是我现场编写的一些代码。
java.util.Date noteTS;
String time, date;
TextView tvTime, tvDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deskclock);
tvTime = (TextView) findViewById(R.id.tvTime);
tvDate = (TextView) findViewById(R.id.tvDate);
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateTextView();
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
private void updateTextView() {
noteTS = Calendar.getInstance().getTime();
String time = "hh:mm"; // 12:00
tvTime.setText(DateFormat.format(time, noteTS));
String date = "dd MMMMM yyyy"; // 01 January 2013
tvDate.setText(DateFormat.format(date, noteTS));
}
答案 3 :(得分:14)
如果您想在textview上显示时间,请更好地使用Chronometer或TextClock
使用天文台:这是在API 1中添加的。它有很多选项可以自定义。
Your xml
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
Your activity
Chronometer mChronometer=(Chronometer) findViewById(R.id.chronometer);
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
使用TestClock :这个小部件是在API级别17中引入的。我个人喜欢Chronometer。
Your xml
<TextClock
android:id="@+id/textClock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:format12Hour="hh:mm:ss a"
android:gravity="center_horizontal"
android:textColor="#d41709"
android:textSize="44sp"
android:textStyle="bold" />
多数民众赞成,你已经完成了。
您可以使用这两个窗口小部件中的任何一个。这将使你的生活变得轻松。
答案 4 :(得分:5)
您可以使用Timer而不是Thread。这是我的全部代码
package dk.tellwork.tellworklite.tabs;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import dk.tellwork.tellworklite.MainActivity;
import dk.tellwork.tellworklite.R;
@SuppressLint("HandlerLeak")
public class HomeActivity extends Activity {
Button chooseYourAcitivity, startBtn, stopBtn;
TextView labelTimer;
int passedSenconds;
Boolean isActivityRunning = false;
Timer timer;
TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_home);
chooseYourAcitivity = (Button) findViewById(R.id.btnChooseYourActivity);
chooseYourAcitivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//move to Activities tab
switchTabInActivity(1);
}
});
labelTimer = (TextView)findViewById(R.id.labelTime);
passedSenconds = 0;
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isActivityRunning) {
//pause running activity
timer.cancel();
startBtn.setText(getString(R.string.homeStartBtn));
isActivityRunning = false;
} else {
reScheduleTimer();
startBtn.setText(getString(R.string.homePauseBtn));
isActivityRunning = true;
}
}
});
stopBtn = (Button)findViewById(R.id.stopBtn);
stopBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
passedSenconds = 0;
labelTimer.setText("00 : 00 : 00");
startBtn.setText(getString(R.string.homeStartBtn));
isActivityRunning = false;
}
});
}
public void reScheduleTimer(){
timer = new Timer();
timerTask = new myTimerTask();
timer.schedule(timerTask, 0, 1000);
}
private class myTimerTask extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
passedSenconds++;
updateLabel.sendEmptyMessage(0);
}
}
private Handler updateLabel = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
int seconds = passedSenconds % 60;
int minutes = (passedSenconds / 60) % 60;
int hours = (passedSenconds / 3600);
labelTimer.setText(String.format("%02d : %02d : %02d", hours, minutes, seconds));
}
};
public void switchTabInActivity(int indexTabToSwitchTo){
MainActivity parentActivity;
parentActivity = (MainActivity) this.getParent();
parentActivity.switchTab(indexTabToSwitchTo);
}
}
答案 5 :(得分:2)
此代码对我有用。
//Get Time and Date
private String getTimeMethod(String formate)
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(formate);
String formattedDate= dateFormat.format(date);
return formattedDate;
}
//this method is used to refresh Time every Second
private void refreshTime() //Call this method to refresh time
{
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
txtV_Time.setText(getTimeMethod("hh:mm:ss a")); //hours,Min and Second with am/pm
txtV_Date.setText(getTimeMethod("dd-MMM-yy")); //You have to pass your DateFormate in getTimeMethod()
};
});
}
}, 0, 1000);//1000 is a Refreshing Time (1second)
}
答案 6 :(得分:1)
使用TextSwitcher
(用于精彩文本过渡动画)和计时器。
答案 7 :(得分:1)
您也可以使用const ReactHighstock = require('react-highcharts/ReactHighstock');
。
这是一种方法
TimerTask
答案 8 :(得分:1)
如果你刚刚使用像这样的定时器运行的AsyncTask
会更好 Timer LAIATT = new Timer();
TimerTask LAIATTT = new TimerTask()
{
@Override
public void run()
{
LoadAccountInformationAsyncTask LAIAT = new LoadAccountInformationAsyncTask();
LAIAT.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
};
LAIATT.schedule(LAIATTT, 0, 1000);