布局文件:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="出生日期:" />
<TextView
android:id="@+id/tv_born"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1987年3月15日" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="showDatePickerDialog"
android:src="@android:drawable/ic_menu_edit" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平均寿命:" />
<TextView
android:id="@+id/tv_lifetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="75" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showDatePickerDialog"
android:text="岁" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="btnMinus"
android:text="-" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="btnAdd"
android:text="+" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="生命进程:" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|right"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_life_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="99999999" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天" />
</LinearLayout>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.pmtoam.lifeline;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends FragmentActivity
{
public final String PREFS_NAME = "prefs_pmtoam";
public static SharedPreferences settings;
public static SharedPreferences.Editor editor;
public static TextView tv_born, tv_lifetime, tv_life_second;
public static ProgressBar mProgressBar;
public static int year, month, day, lifetime;
public static int max; // 总生命天数
public static long life_second;
public static Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
tv_born = (TextView) findViewById(R.id.tv_born);
tv_lifetime = (TextView) findViewById(R.id.tv_lifetime);
tv_life_second = (TextView) findViewById(R.id.tv_life_second);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
settings = getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
getSP();
setPgb();
}
public static void setPgb()
{
index = 1;
String startdate = year + "-" + month + "-" + day;
String enddate = (year + lifetime) + "-" + month + "-" + day;
max = (int) getQuot(startdate, enddate);
mProgressBar.setMax(max);
final Calendar c = Calendar.getInstance();
int _year = c.get(Calendar.YEAR);
int _month = c.get(Calendar.MONTH);
int _day = c.get(Calendar.DAY_OF_MONTH);
String _startdate = year + "-" + month + "-" + day;
String _enddate = _year + "-" + _month + "-" + _day;
int _max = (int) getQuot(_startdate, _enddate);
mProgressBar.setProgress(_max);
life_second = max - _max;
tv_life_second.setText(life_second + "");
mHandler.removeCallbacks(mRunnable2);
mHandler.post(mRunnable2);
}
public static void getSP()
{
year = settings.getInt("year", 1987);
month = settings.getInt("month", 2);
day = settings.getInt("day", 15);
lifetime = settings.getInt("lifetime", 75);
if (lifetime > 150)
lifetime = 150;
tv_born.setText(year + "年" + (month + 1) + "月" + day + "日");
tv_lifetime.setText(lifetime + "");
}
@Override
protected void onDestroy()
{
mHandler.removeCallbacks(mRunnable2);
super.onDestroy();
}
static int index = 1;
public static Runnable mRunnable2 = new Runnable()
{
@Override
public void run()
{
int per = max / 100;
index++;
if (index > 100)
index = 1;
mProgressBar.setSecondaryProgress(per * index);
mHandler.postDelayed(mRunnable2, 20);
}
};
public void showDatePickerDialog(View v)
{
android.support.v4.app.DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void btnAdd(View v)
{
lifetime++;
if (lifetime > 150)
lifetime = 150;
tv_lifetime.setText(lifetime + "");
editor.putInt("lifetime", lifetime);
editor.commit();
setPgb();
}
public void btnMinus(View v)
{
lifetime--;
if (lifetime > 150)
lifetime = 150;
tv_lifetime.setText(lifetime + "");
editor.putInt("lifetime", lifetime);
editor.commit();
setPgb();
}
/**
* 返回两个时间相差的天数
*
* @param startdate 格式yyyy-MM-dd
* @param enddate 格式yyyy-MM-dd
* @return
*/
public static long getQuot(String startdate, String enddate)
{
long day = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date date1 = format.parse(startdate);
Date date2 = format.parse(enddate);
day = date2.getTime() - date1.getTime();// 计算所得为微秒数
day = day / 1000 / 60 / 60 / 24;// 计算所得的天数
}
catch (Exception e)
{
e.printStackTrace();
}
return day;
}
}
DatePickerFragment.java
package com.pmtoam.lifeline;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements
android.app.DatePickerDialog.OnDateSetListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
MainActivity.getSP();
return new DatePickerDialog(getActivity(), this, MainActivity.year,
MainActivity.month, MainActivity.day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
{
MainActivity.editor.putInt("year", year);
MainActivity.editor.putInt("month", monthOfYear);
MainActivity.editor.putInt("day", dayOfMonth);
MainActivity.editor.commit();
MainActivity.getSP();
MainActivity.setPgb();
}
}