Android如何制作像倒计时时钟一样的日历?

标签 android

我在我的应用程序中使用倒数计时器,其中我以简单的日期格式显示日期,但我的倒数计时器没有显示,简单的时钟在其中工作,我想像倒数计时器一样显示我的日历。这是我的代码。

 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;
    }
}


  }

谢谢。

最佳答案

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如何制作像倒计时时钟一样的日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21701393/

相关文章:

java - Null 对象引用上的 SupportMapFragment#getMapAsync()

安卓 onTouchEvent。不同安卓版本的不同行为

android - 如何从kml层android获取点列表

android - 在此处创建 API Android 请求 ID?

android - 尝试使用 Android AsyncTask 从服务器获取响应

java - SimpleDateFormat 和日期比较未给出正确的输出

Android P - 从 Assets 复制数据库后出现 'SQLite: No Such Table Error'

android - 是否可以在每个不同用户的开始日期和结束日期之间的第 3 个月自动从服务器端 Java 发送推送通知?

android - Dagger2 的项目结构

android - 如何从Android中的FCM数据消息中获取值(value)?