我是Android工作室的初学者,并尝试制作年龄计算器的应用程序,但我不明白我如何计算从今天起的下一个生日。我尝试了很多代码和功能,但是在运行应用程序后,我的应用程序自动关闭了这些功能和代码。 我分享代码请帮我解决。
public class MainActivity extends AppCompatActivity {
// Inatialized the code for the navigation bar menu
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mToggal;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
// Inatialized the Code for the calendar
ImageButton btnCelandar1;
ImageButton btnCelandar2;
int year_x, month_x, day_x;
static final int DIALOD_ID = 0, DIALOG_ID = 1;
EditText etYeay, etMonth, etDay;
int year_y, month_y = 12, day_y = 31;
int dayOf_Week;
////// User Input For Date Section ////////////
EditText uiDay, uiMonth, uiYear;
///////// Day View Inatializer For Current and User Input ////////
TextView dayView, userInputDayView;
//////////////************ Code for Button To calculate the age******/////////
TextView tvYearAge, tvMonthAge, tvDaysAge;
TextView tvBirthMonth, tvBirthDays;
TextView tvExtraYear, tvExtraMonth, tvExtraWeeks, tvExtraDays, tvExtraHours, tvExtraMinutes, tvExtraSeconds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initalized the paremeters
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggal = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
//staring up the navigation menur
drawerLayout.addDrawerListener(mToggal);
mToggal.syncState();
//show up the botton
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// fragmentTransaction = getSupportFragmentManager().beginTransaction();
//fragmentTransaction.add(R.id.main_container,new SettingsFragment());
// fragmentTransaction.commit();
//getSupportActionBar().setTitle("Home Fragment");
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
Intent iSetting = new Intent(MainActivity.this , SettingActivity.class);
startActivity(iSetting);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.about:
Intent iAbout = new Intent(MainActivity.this , AboutActivity.class);
startActivity(iAbout);
item.setChecked(true);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
/// Coding for the Calendar Button start hear
shoDialogOnButtonClick();
//////////////// Path For The Initializer Day View///////////
dayView = (TextView) findViewById(R.id.current_day_view);
userInputDayView = (TextView) findViewById(R.id.userInput_DayView);
/// coding for the textview on edit text
etYeay = (EditText) findViewById(R.id.date_year_view);
etMonth = (EditText) findViewById(R.id.date_month_view);
etDay = (EditText) findViewById(R.id.date_day_view);
//***///
///////Inatialized the variable Of user input EditText//////////////////////////
uiYear = (EditText) findViewById(R.id.etYear_By_user);
uiMonth = (EditText) findViewById(R.id.etMonth_By_User);
uiDay = (EditText) findViewById(R.id.etDay_By_User);
/////********** Current Date Method For Calendar 1*******/////////
final Calendar calendar = Calendar.getInstance();
year_x = calendar.get(Calendar.YEAR);
month_x = calendar.get(Calendar.MONTH);
day_x = calendar.get(Calendar.DAY_OF_MONTH);
dayOf_Week = calendar.get(Calendar.DAY_OF_WEEK);
String weekday = new DateFormatSymbols().getWeekdays()[dayOf_Week];
dayView.setText("" + weekday);
etYeay.setText("" + year_x);
etMonth.setText("" + (month_x + 1));
etDay.setText("" + day_x);
/////////*************** Current Date Method For The Calendar 2 *******/////////////
final Calendar calendar2 = Calendar.getInstance();
year_y = calendar.get(Calendar.YEAR);
month_y = calendar.get(Calendar.MONTH);
day_y = calendar.get(Calendar.DAY_OF_MONTH);
///////////********* Initialized the path of resulting Factor*******//////////
tvYearAge = (TextView) findViewById(R.id.result_age_year);
tvMonthAge = (TextView) findViewById(R.id.result_age_months);
tvDaysAge = (TextView) findViewById(R.id.resuls_age_days);
tvBirthMonth = (TextView) findViewById(R.id.next_birthday_months);
tvBirthDays = (TextView) findViewById(R.id.next_birthday_days);
tvExtraYear = (TextView) findViewById(R.id.result_Extra_Years);
tvExtraMonth = (TextView) findViewById(R.id.result_Extra_Months);
tvExtraWeeks = (TextView) findViewById(R.id.result_Extra_Weeks);
tvExtraDays = (TextView) findViewById(R.id.result_Extra_days);
tvExtraHours = (TextView) findViewById(R.id.result_Extra_Hours);
////////##*******##////////////////
}
public void CalculateAge(View view) {
String currentyearET = etYeay.getText().toString();
String userInputyearET = uiYear.getText().toString();
String currentMonthET = etMonth.getText().toString();
String userInputMonthET = uiMonth.getText().toString();
String currentDayET = etDay.getText().toString();
String userInputDayET = uiDay.getText().toString();
///////////////////////***********//////////////////
int currentAgeInYear = Integer.parseInt(currentyearET) - Integer.parseInt(userInputyearET);
int currentAgeInMonth = Integer.parseInt(currentMonthET) - Integer.parseInt(userInputMonthET);
int currentAgeInDays = Integer.parseInt(currentDayET) - Integer.parseInt(userInputDayET);
/////////////////*********//////////////
tvYearAge.setText("" + currentAgeInYear);
tvMonthAge.setText("" + currentAgeInMonth);
tvDaysAge.setText("" + currentAgeInDays);
/////////////////////////**********Code For Next Birth Day ************///////////////////////////
/////////////*************//////////////////////////***///
int extraAgeInMonths = (currentAgeInYear*12)+currentAgeInMonth;
int extraAgeInWeeks = (int) (extraAgeInMonths * 4.35)+(currentAgeInDays / 7);
int extraAgeInDays = (int) ((currentAgeInYear*365.25)+(currentAgeInMonth * 30.45)+currentAgeInDays);
int extraAgeInHours = (extraAgeInDays)* 24;
tvExtraYear.setText(""+currentAgeInYear);
tvExtraMonth.setText(""+extraAgeInMonths);
tvExtraWeeks.setText(""+extraAgeInWeeks);
tvExtraDays.setText(""+extraAgeInDays);
tvExtraHours.setText(""+extraAgeInHours);
}
}