单击按钮后跳转2

时间:2016-04-20 17:08:26

标签: android

为什么当我第一次点击按钮时它的跳跃2个月但是点击它后1点1然后当我点击另一个按钮时它做同样的事情。我做得不好?我粘贴所有代码,因为我不知道哪里有问题。 `

public class MainActivity extends AppCompatActivity {
static int month;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView text3 = (TextView) findViewById(R.id.textView);
    TextView text2 = (TextView) findViewById(R.id.textView10);
    final TextView text = (TextView) findViewById(R.id.textView8);
    final TextView text1 = (TextView) findViewById(R.id.textView9);
    final Button button0 = (Button) findViewById(R.id.button);
    final Button button1 = (Button) findViewById(R.id.button2);
    final Button button2 = (Button) findViewById(R.id.button3);
   final Button button3 = (Button) findViewById(R.id.button4);
   final Button button4 = (Button) findViewById(R.id.button5);
   final Button button5 = (Button) findViewById(R.id.button6);
   final Button button6 = (Button) findViewById(R.id.button7);
   final Button button7 = (Button) findViewById(R.id.button8);
   final Button button8 = (Button) findViewById(R.id.button9);
   final Button button9 = (Button) findViewById(R.id.button10);
   final Button button10 = (Button) findViewById(R.id.button11);
   final Button button11 = (Button) findViewById(R.id.button12);
   final Button button12 = (Button) findViewById(R.id.button13);
   final Button button13 = (Button) findViewById(R.id.button14);
   final Button button14 = (Button) findViewById(R.id.button15);
   final Button button15 = (Button) findViewById(R.id.button16);
   final Button button16 = (Button) findViewById(R.id.button17);
   final Button button17 = (Button) findViewById(R.id.button18);
   final Button button18 = (Button) findViewById(R.id.button19);
   final Button button19 = (Button) findViewById(R.id.button20);
   final Button button20 = (Button) findViewById(R.id.button21);
   final Button button21 = (Button) findViewById(R.id.button22);
   final Button button22 = (Button) findViewById(R.id.button23);
   final Button button23 = (Button) findViewById(R.id.button24);
   final Button button24 = (Button) findViewById(R.id.button25);
   final Button button25 = (Button) findViewById(R.id.button26);
   final Button button26 = (Button) findViewById(R.id.button27);
   final Button button27 = (Button) findViewById(R.id.button28);
   final Button button28 = (Button) findViewById(R.id.button29);
   final Button button29 = (Button) findViewById(R.id.button30);
   final Button button30 = (Button) findViewById(R.id.button31);
   final Button button31 = (Button) findViewById(R.id.button32);
   final Button button32 = (Button) findViewById(R.id.button33);
   final Button button33 = (Button) findViewById(R.id.button34);
   final Button button34 = (Button) findViewById(R.id.button35);
   final Button button35 = (Button) findViewById(R.id.button36);
   final Button button36 = (Button) findViewById(R.id.button37);
    Button buttonleft = (Button) findViewById(R.id.button38);
    final Button buttonright = (Button) findViewById(R.id.button39);
    Calendar c = Calendar.getInstance();
    int Realmonth = c.get(Calendar.MONTH);
    final Date date = new Date();
    float jobhours = 7.5f;
    float workdays = 31;
    float totalhours;
    final int[] realyear = {c.get(Calendar.YEAR)};
    final GregorianCalendar calendar = new GregorianCalendar(realyear[0], month, 1);
    int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
    final int day = calendar.get(calendar.DAY_OF_WEEK);
    if (Realmonth == 0) {
        text.setText(getResources().getString(R.string.january));
        month = 1;
    }
    else if (Realmonth == 1) {
        text.setText(getResources().getString(R.string.February));
        month = 2;
    }
    else if (Realmonth == 2) {
        text.setText(getResources().getString(R.string.marec));
        month = 3;
    }
    else if (Realmonth == 3) {
        text.setText(getResources().getString(R.string.april));
        month = 4;
    }
    else if (Realmonth == 4) {
        text.setText(getResources().getString(R.string.may));
        month = 5;
    }
    else if (Realmonth == 5) {
        text.setText(getResources().getString(R.string.Juny));
        month = 6;
    }
    else if (Realmonth == 6) {
        text.setText(getResources().getString(R.string.jul));
        month = 7;
    }
    else if (Realmonth == 7) {
        text.setText(getResources().getString(R.string.august));
        month = 8;
    }
    else if (Realmonth == 8) {
        text.setText(getResources().getString(R.string.september));
        month = 9;
    }
    else if (Realmonth == 9) {
        text.setText(getResources().getString(R.string.oktober));
        month = 10;
    }
    else if (Realmonth == 10) {
        text.setText(getResources().getString(R.string.november));
        month = 11;
    }
    else if (Realmonth == 11) {
        text.setText(getResources().getString(R.string.december));
        month = 12;
    }
    assert buttonleft != null;
    buttonleft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            month--;

            if (month == 0) {

                month = 12;
               text.setText(getResources().getString(R.string.december));
                realyear[0] = realyear[0] - 1;
                text1.setText("" + realyear[0]);

            }
           else if (month == 1) {
                text.setText(getResources().getString(R.string.january));
            }
           else if (month == 2) {                    text.setText(getResources().getString(R.string.February));
            }
            else if (month == 3) {
                text.setText(getResources().getString(R.string.marec));
            }
            else if (month == 4) {
                text.setText(getResources().getString(R.string.april));
            }
            else if (month == 5) {
                text.setText(getResources().getString(R.string.may));
            }
            else if (month == 6) {
                text.setText(getResources().getString(R.string.Juny));
            }
            else if (month == 7) {
                text.setText(getResources().getString(R.string.jul));
            }
            else if (month == 8) {
                text.setText(getResources().getString(R.string.august));
            }
            else if (month == 9) {
                text.setText(getResources().getString(R.string.september));
            }
            else if (month == 10) {
                text.setText(getResources().getString(R.string.oktober));
            }
            else if (month == 11) {
                text.setText(getResources().getString(R.string.november));
            }
            else if (month == 12) {
                text.setText(getResources().getString(R.string.december));
            }
}
            }
        }
    });

    assert buttonright != null;
    buttonright.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            month++;

            if (month == 12) {
                realyear[0] = realyear[0] + 1;
                month = 0;
                text.setText(getResources().getString(R.string.january));

                text1.setText("" + realyear[0]);
            }
            else if (month == 0) {
                text.setText(getResources().getString(R.string.january));
            }
            else if (month == 1) {
                text.setText(getResources().getString(R.string.February));
            }
            else if (month == 2) {
                text.setText(getResources().getString(R.string.marec));
            }
            else if (month == 3) {
                text.setText(getResources().getString(R.string.april));
            }
            else if (month == 4) {
                text.setText(getResources().getString(R.string.may));
            }
            else if (month == 5) {
                text.setText(getResources().getString(R.string.Juny));
            }
            else if (month == 6) {
                text.setText(getResources().getString(R.string.jul));
            }
            else if (month == 7) {
                text.setText(getResources().getString(R.string.august));
            }
            else if (month == 8) {
                text.setText(getResources().getString(R.string.september));
            }
            else if (month == 9) {
                text.setText(getResources().getString(R.string.oktober));
            }
            else if (month == 10) {
                text.setText(getResources().getString(R.string.november));
            }
            else if (month == 11) {
                text.setText(getResources().getString(R.string.december));
            }

        }

    });

    if (month == 2)
    {
        if (realyear[0] % 4 == 0) {
            workdays = 29;
        }
        workdays = 28;

        totalhours = jobhours * workdays;
        text2.setText("Odrobené hodiny:" + totalhours);


    }
    else if (month == 6) {
        workdays = 30;
        totalhours = jobhours * workdays;
        text2.setText("Odrobené hodiny:" + totalhours);
    }
    else if (month == 9) {
        workdays = 30;
        totalhours = jobhours * workdays;
        text2.setText("Odrobené hodiny:" + totalhours);
    }
    else if (month == 11) {
        workdays = 30;
        totalhours = jobhours * workdays;
        text2.setText("Odrobené hodiny:" + totalhours);
    }
    else {
        workdays = 31;
        totalhours = jobhours * workdays;
        text2.setText("Odrobené hodiny:" + totalhours);
    }
}

}`

2 个答案:

答案 0 :(得分:1)

这是因为“GregorianCalendar”甚至“Date”类的月份为0,而不是1。即0到11。

在您的代码中:

int Realmonth = c.get(Calendar.MONTH);

将其更改为:

int Realmonth = c.get(Calendar.MONTH)+1;

就是这样。从第一次点击开始,月份将逐一改变。

答案 1 :(得分:0)

我自己解决了Avinash有点真实我必须在0到1之间改变每一个值。