我在将特定时间设置Button
为不可见时遇到了麻烦CardView
我想要达到的目标:我有CardView
一个Button
。用户下订单时,订单日期和时间存储在MySQL服务器数据库中。我从服务器获取此时间,并向其添加了10分钟的延迟。
现在,考虑这次使Button
invisble / gone消失了CardView
。
我使用的是什么:对于时候,我使用约达时间及任何下面的代码是内部onBindViewHolder()
的Recyclerview
到目前为止,我已经尝试过:我从服务器上得到了时间
String orderDate = cOrder.getOrderDate(); // 2016-08-18 00:02:32
然后将时间转换为Date
格式
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = dateTimeFormatter.parseDateTime(orderDate);
我使用以下时间增加10分钟的时间来设置延迟时间
DateTime delay = dt.plusMinutes(10);
现在我在Button
使用上设置延迟时间postDelayed()
holder.btnCancel.postDelayed(new Runnable() {
@Override
public void run() {
holder.btnCancel.setVisibility(View.GONE);
}
}, delay.getMillis());
现在,当我运行该应用程序时,该按钮上的按钮CardView
应已消失(如orderDate所示)(8月18日-当前日期前两天)。
我也尝试使用常规Java时间代替JODA,但是没有运气。
我无法弄清楚问题是什么。要求您的指导。
谢谢
从以下文档中postDelayed()
:
delayMillis
long
:直到Runnable
将被执行的延迟(以毫秒为单位)。
让我们添加10分钟2016-08-18 00:02:32
,并将其转换为毫秒(从纪元开始)。等于像这样的东西1471471952000
。
您将此值传递postDelayed()
为延迟,有效地告诉毫秒后才Handler
执行此操作,这大约等于47年。Runnable
1471471952000
那真的没有道理。
要隐藏Button
给定值Date
是否在current之前Date
,可以执行以下操作:
String orderDateString = cOrder.getOrderDate();
try {
Date now = new Date();
Date orderDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.getDefault()).parse(orderDateString);
if(orderDate.before(now)) {
holder.btnCancel.setVisibility(View.GONE);
}
} catch (ParseException pe) {
pe.printStackTrace();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句