Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获得年龄并将其用于其他活动?_Java_Android - Fatal编程技术网

Java 如何获得年龄并将其用于其他活动?

Java 如何获得年龄并将其用于其他活动?,java,android,Java,Android,我正在为婴儿开发一个安卓应用程序,在用户设置婴儿的出生日期后,我需要保存它,以便在睡眠和接种疫苗等其他活动中使用它。 我使用共享首选项保存用户输入的数据。 但我还没有弄明白如何获取这些数据并在其他类中使用。你能帮我吗 以下是所需婴儿数据的代码 public class MainActivity extends Activity { private Button change; private ImageView bImage; private Button mPickDate; private

我正在为婴儿开发一个安卓应用程序,在用户设置婴儿的出生日期后,我需要保存它,以便在睡眠和接种疫苗等其他活动中使用它。 我使用共享首选项保存用户输入的数据。 但我还没有弄明白如何获取这些数据并在其他类中使用。你能帮我吗

以下是所需婴儿数据的代码

public class MainActivity extends Activity {
private Button change;
private ImageView bImage;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
     // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        updateDateDisplay();


}
   private void updateDateDisplay() {

        this.mPickDate.setText(
        new StringBuilder()
        .append(mMonth + 1).append("-")
        .append(mDay).append("-")
        .append(mYear).append(" "));
        }
   public int getAge (int _year, int _month, int _day) {

       GregorianCalendar cal = new GregorianCalendar();
       int y, m, d, a;         

       y = cal.get(Calendar.YEAR);
       m = cal.get(Calendar.MONTH) + 1;
       d = cal.get(Calendar.DAY_OF_MONTH);
       cal.set(_year, _month, _day);
       a = y - cal.get(Calendar.YEAR);
       if ((m < cal.get(Calendar.MONTH))
                       || ((m == cal.get(Calendar.MONTH)) && (d < cal
                                       .get(Calendar.DAY_OF_MONTH)))) {
               --a;
       }
       if(a < 0)
               throw new IllegalArgumentException("Age < 0");
       return a;
       }



    // the callback received when the user “sets” the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year,
    int monthOfYear, int dayOfMonth) {
    mYear = year;
    mMonth = monthOfYear;
    mDay = dayOfMonth;
    updateDateDisplay();
    }

    };


    @Override
    protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
    return new DatePickerDialog(this,
    mDateSetListener,
    mYear, mMonth, mDay);
    }
    return null;
    }
公共类MainActivity扩展活动{
私人按钮更改;
私人图像视图双图像;
私有按钮mPickDate;
私人髓鞘内;
私人住宅;
私人国际日;
静态最终整数日期对话框ID=0;
//获取当前日期
最终日历c=Calendar.getInstance();
mYear=c.get(日历年);
mMonth=c.get(日历月);
mDay=c.get(日历,月的第天);
UpdateDataDisplay();
}
私有void updateDataDisplay(){
this.mPickDate.setText(
新的StringBuilder()
.append(mm+1)。append(“-”)
.append(mDay)。append(“-”)
.append(mYear)。append(“”);
}
公共整数获取(整数年、整数月、整数日){
GregorianCalendar cal=新的GregorianCalendar();
int y,m,d,a;
y=cal.get(日历年);
m=cal.get(日历月)+1;
d=校准获取(日历日,每月);
校准设置(年、月、日);
a=y-校准获取(日历年);
如果((m
由于我在您的代码中看不到您在哪里创建了
共享参考
,因此我将从


只需使用保存时创建的pref的名称即可。此示例检索一个
布尔值
,但您可以轻松地将其更改为
整数
字符串
或访问共享Prefs所需的任何内容->

用于将
String
数据保存在prefs->

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor e = prefs.edit();
e.putString(key, val);   //Key - Name with which you want to save pref, val -  value to save
e.commit();
用于从prefs中获取
字符串
数据->

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String s = prefs.getString(key, def); // Key - Name of prefs, def - default value if prefs with specified key so not exists

您可以在prefs中保存
String
Float
Long
Int
Boolean
等。获取年龄,然后使用intent在活动之间传递数据,或者打开应用程序的相同共享首选项并读取其中保存的日期。

为什么不通过intent将数据传递给其他活动?似乎这比为一段数据使用SharedPrefs要简单得多非常感谢你的可能副本我会尝试。我制作了另一个只扩展首选项活动的类,我将包含数据字段的xml布局添加到该类中,它起到了作用。当数据输入时,它确实保存了数据。好的,如果我想跟踪年龄并不断更新给用户,那么我应该添加什么呢?好的,现在我制作了一个全新的代码,将出生日期作为编辑文本写入这里是我的pref.xml的代码
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String s = prefs.getString(key, def); // Key - Name of prefs, def - default value if prefs with specified key so not exists