Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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/6/eclipse/9.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
Android 设置日历中的年和月_Android_Eclipse_Calendar - Fatal编程技术网

Android 设置日历中的年和月

Android 设置日历中的年和月,android,eclipse,calendar,Android,Eclipse,Calendar,我正在尝试将日历设置为用户使用2个EditText输入的年和月,由于某些原因,我无法更改日历。下面是我正在研究的方法。有人知道我哪里出错了吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button change2 = (Button) findViewByI

我正在尝试将日历设置为用户使用2个EditText输入的年和月,由于某些原因,我无法更改日历。下面是我正在研究的方法。有人知道我哪里出错了吗

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button change2 = (Button) findViewById(R.id.button1);

    change2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            change();
        }
    });
}

public void change() {
    Calendar currentDate = GregorianCalendar.getInstance();

    Month = (EditText) findViewById(R.id.editText1);
    Year = (EditText) findViewById(R.id.editText2);

    int newmonth = Integer.parseInt(Month.getText().toString());
    int newyear = Integer.parseInt(Year.getText().toString());

    currentDate.set (Calendar.YEAR, newyear);
    currentDate.set (Calendar.MONTH, newmonth);
}

Android日历API中的月份基于零索引,因此您需要:

public void change() {
  Calendar currentDate = GregorianCalendar.getInstance();

  Month = (EditText) findViewById(R.id.editText1);
  Year = (EditText) findViewById(R.id.editText2);

  int newmonth = Integer.parseInt(Month.getText().toString());
  int newyear = Integer.parseInt(Year.getText().toString());

  currentDate.set (Calendar.YEAR, newyear);
  currentDate.set (Calendar.MONTH, newmonth - 1); // here the change
}
您还应该检查您的方法
change()
是否在合适的侦听器中被调用,请注意。有关如何使用它的示例,请参见

重要更新:

不要忘记使用局部变量
currentDate
更新UI。您将其设置到现在,然后在方法
change()
结束后将其丢弃。你可以用谷歌搜索,因为我不是安卓专家(只是个日历专家)。然而,我发现这两个贡献可能会给你一个想法:


您好,您建议我做了更改,但就更改日历而言,它仍然没有做任何事情。我已经编辑了这个问题,以包括我的oncreate方法,我不确定这是否与侦听器有关。@Mark101请查看我的更新。使用变量
currentDate
,您最终会做什么?我不确定如何更新它,我试图研究如何更新,但没有成功。我看到的所有示例都停在currentDate.set行。@Mark101我添加了两个SO链接,希望对您有所帮助。