本文介绍了Android的如何使压延像倒计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用countdowntimer在我的应用程序,其中我显示简单的日期格式的日期,但我countdowntimer没有显示,简单的时钟是在工作,我想表明我喜欢压延是countdowntimer.Here我的code
公共类MainActivity延伸活动{
ListView的mListView;
MyCountDownTimer mMyCountDownTimer;
TextView的文本1;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); mListView =(ListView控件)findViewById(R.id.listView);
MyAdapter适配器=新MyAdapter(本);
mListView.setAdapter(适配器); mMyCountDownTimer =新MyCountDownTimer(1000,1000,适配器);
}
@覆盖
保护无效onResume(){ super.onResume();
mMyCountDownTimer.start();
}@覆盖
保护无效的onPause(){ mMyCountDownTimer.cancel();
super.onPause();
}
// countdowntimerclass
公共类MyCountDownTimer扩展CountDownTimer { BaseAdapter mAdapter; 公共MyCountDownTimer(长millisInFuture,长countDownInterval,BaseAdapter适配器){ 超(millisInFuture,countDownInterval);
mAdapter =适配器;
} @覆盖
公共无效onFinish(){ mAdapter.notifyDataSetChanged();
this.start();
} @覆盖
公共无效onTick(长millisUntilFinished){
text1.setText(+ millisUntilFinished / 1000);
}
}
公共类MyAdapter延伸BaseAdapter { 私有静态最终诠释COUNT = 50;
私人布尔firstscroll = TRUE;
私人上下文的背景下;
私人日期格式日期格式; 公共MyAdapter(上下文的背景下){ this.context =背景; } @覆盖
公众诠释的getCount(){ 返回计数;
} @覆盖
公共字符串的getItem(INT位置){ 回归项目+将String.valueOf(位置);
} @覆盖
众长getItemId(INT位置){ 返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 如果(convertView == NULL){
convertView = LayoutInflater.from(的getContext()).inflate(android.R.layout.two_line_list_item,父母,假);
}
日历C = Calendar.getInstance();
的System.out.println(当前时间=>中+ c.getTime()); SimpleDateFormat的DF =新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS,Locale.US);
串formattedDate = df.format(c.getTime());
文本1 =(TextView中)convertView.findViewById(android.R.id.text1);
如果(位置== 0){ text1.setText(的getItem(位置).valueOf(formattedDate));
}
其他{
text1.setText(NULL);
} 返回convertView;
} 私人CharSequence的getTimeString(INT位置){ 如果(位置%2 == 0){ //返回dateFormat.format(Calendar.getInstance()的getTime());
}
其他{
返回null;
}
返回null;
} 民营背景的getContext(){ 返回语境;
}
}
}
感谢您。
解决方案
包com.example.diffrence;进口java.text.ParseException;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口java.util.Timer中;
进口java.util.TimerTask中;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.widget.LinearLayout;
进口android.widget.TextView;公共类MainActivity延伸活动{ TextView的TXT;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
TXT =(的TextView)findViewById(R.id.test);
定时器notificationtask =新的Timer(); notificationtask.schedule(新的TimerTask(){
@覆盖
公共无效的run(){
// TODO自动生成方法存根
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){
// TODO自动生成方法存根
的SimpleDateFormat SimpleDateFormat的=
新的SimpleDateFormat(DD / M / YYYY HH:MM:SS);
尝试{
日期日期1 = simpleDateFormat.parse(2014年10月10日11时三十零分10秒); 字符串currentDateandTime = simpleDateFormat.format(新的Date());
日期DATE2 = simpleDateFormat.parse(currentDateandTime);
txt.setText(时间Remainig迄今为止二〇一四年十月一十日11时三十分10秒:+ printDifference(DATE1,DATE2)); }赶上(ParseException的E){
// TODO自动生成catch块
e.printStackTrace();
}
}
});
}
},1000,1000);
} 公共字符串printDifference(日期的startDate,日期结束日期){ //毫秒
长不同= endDate.getTime() - startDate.getTime(); 的System.out.println(开始日期:+的startDate);
的System.out.println(结束日期:+结束日期);
的System.out.println(不同:+不同); 长secondsInMilli = 1000;
长minutesInMilli = secondsInMilli * 60;
长hoursInMilli = minutesInMilli * 60;
长daysInMilli = hoursInMilli * 24; 长elapsedDays =不同/ daysInMilli;
不同的=不同的%daysInMilli; 长elapsedHours =不同/ hoursInMilli;
不同的=不同的%hoursInMilli; 长elapsedMinutes =不同/ minutesInMilli;
不同的=不同的%minutesInMilli; 长elapsedSeconds =不同/ secondsInMilli;
返回Math.abs(elapsedDays)+天+
Math.abs(elapsedHours)+小时+
Math.abs(elapsedMinutes)+分+
Math.abs(elapsedSeconds)+秒; }
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}}
I am using countdowntimer in my application,in which I am showing date in simple dateformat but my countdowntimer is not showing , simple clock is working in that, I want to show my calender like countdowntimer.Here is my code.
public class MainActivity extends Activity {
ListView mListView;
MyCountDownTimer mMyCountDownTimer;
TextView text1;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mListView = (ListView) findViewById( R.id.listView );
MyAdapter adapter = new MyAdapter( this);
mListView.setAdapter( adapter );
mMyCountDownTimer = new MyCountDownTimer( 1000, 1000, adapter );
}
@Override
protected void onResume() {
super.onResume();
mMyCountDownTimer.start();
}
@Override
protected void onPause() {
mMyCountDownTimer.cancel();
super.onPause();
}
//countdowntimerclass
public class MyCountDownTimer extends CountDownTimer {
BaseAdapter mAdapter;
public MyCountDownTimer(long millisInFuture, long countDownInterval, BaseAdapter adapter) {
super( millisInFuture, countDownInterval );
mAdapter = adapter;
}
@Override
public void onFinish() {
mAdapter.notifyDataSetChanged();
this.start();
}
@Override
public void onTick( long millisUntilFinished) {
text1.setText("" + millisUntilFinished / 1000);
}
}
public class MyAdapter extends BaseAdapter {
private static final int COUNT = 50;
private boolean firstscroll=true;
private Context context;
private DateFormat dateFormat;
public MyAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return COUNT;
}
@Override
public String getItem( int position ) {
return "Item " + String.valueOf( position);
}
@Override
public long getItemId( int position ) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
if ( convertView == null ) {
convertView = LayoutInflater.from( getContext() ).inflate( android.R.layout.two_line_list_item, parent, false );
}
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
String formattedDate = df.format(c.getTime());
text1 = (TextView) convertView.findViewById( android.R.id.text1 );
if (position==0 ) {
text1.setText(getItem(position).valueOf(formattedDate));
}
else {
text1.setText(null);
}
return convertView;
}
private CharSequence getTimeString( int position ) {
if ( position % 2 == 0 ) {
// return dateFormat.format( Calendar.getInstance().getTime() );
}
else {
return null;
}
return null;
}
private Context getContext() {
return context;
}
}
}
thank you.
解决方案
package com.example.diffrence;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.test);
Timer notificationtask = new Timer();
notificationtask.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd/M/yyyy hh:mm:ss");
try {
Date date1 = simpleDateFormat.parse("10/10/2014 11:30:10");
String currentDateandTime = simpleDateFormat.format(new Date());
Date date2 = simpleDateFormat.parse(currentDateandTime);
txt.setText("time Remainig to date 10/10/2014 11:30:10 : " + printDifference(date1,date2));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}, 1000, 1000);
}
public String printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
return Math.abs(elapsedDays) + " days " +
Math.abs(elapsedHours)+ " hours " +
Math.abs(elapsedMinutes)+ " minute " +
Math.abs(elapsedSeconds) + " Seconds ";
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这篇关于Android的如何使压延像倒计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!