我想在 TextView 中实时显示时间和日期(按分钟更新)。目前,我有这个。考虑到内存使用和 Android 最佳实践,这是最好的方法吗? (注意:DateFormat 是 java.text.DateFormat)
private Thread dtThread;
public void onCreate(Bundle savedInstanceState) {
...
getDateAndTime();
}
private void getDateAndTime() {
dtThread = new Thread( new Runnable() {
@Override
public void run() {
Log.d(TAG, "D/T thread started");
while (!Thread.currentThread().isInterrupted()) {
try {
update();
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.d(TAG, "D/T thread interrupted");
}
}
}
public void update() {
runOnUiThread( new Runnable() {
@Override
public void run() {
Date d = new Date();
String time = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(d);
String date = DateFormat.getDateInstance(DateFormat.LONG).format(d);
TextView timeView = (TextView) findViewById(R.id.textStartTime);
TextView dateView = (TextView) findViewById(R.id.textStartDate);
timeView.setText(time);
dateView.setText(date);
}
});
}
});
dtThread.start();
}
protected void onPause() {
super.onPause();
dtThread.interrupt();
dtThread = null;
}
protected void onResume() {
super.onResume();
getDateAndTime();
}
最佳答案
我会使用 Runnable 并将其延迟发送到处理程序。
public class ClockActivity extends Activity {
private SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
private TextView mClock;
private boolean mActive;
private final Handler mHandler;
private final Runnable mRunnable = new Runnable() {
public void run() {
if (mActive) {
if (mClock != null) {
mClock.setText(getTime());
}
mHandler.postDelayed(mRunnable, 1000);
}
}
};
public ClockActivity() {
mHandler = new Handler();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mClock = (TextView) findViewById(R.id.clock_textview);
startClock();
}
private String getTime() {
return sdf.format(new Date(System.currentTimeMillis()));
}
private void startClock() {
mActive = true;
mHandler.post(mRunnable);
}
}
关于java - 在 Android 中按秒更新时间和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400846/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我