Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.distanceindays;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.NumberPicker;
- import java.util.Calendar;
- public class InputDateActivity extends AppCompatActivity {
- Intent mCaller;
- NumberPicker mNpYear, mNpMonth, mNpDay;
- Button mBtnConfirmDate, mBtnCancel;
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnConfirmDate:
- actionConfirmDate();
- break;
- case R.id.idBtnCancel:
- actionCancel();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void actionConfirmDate(){
- Intent intentResult = new Intent(
- InputDateActivity.this,
- MainActivity.class
- );
- int y,m,d;
- y = mNpYear.getValue();
- m = mNpMonth.getValue();
- d = mNpDay.getValue();
- String strDate = String.format(
- "%d-%d-%d", y,m,d
- );
- //like this
- intentResult.putExtra("DATE", strDate);
- //or like this
- intentResult.putExtra("RESULT_YEAR", y);
- intentResult.putExtra("RESULT_MONTH", m);
- intentResult.putExtra("RESULT_DAY", d);
- int iToWhichDateAmIRespondingTo = 1;
- if (mCaller!=null){
- iToWhichDateAmIRespondingTo =
- mCaller.getIntExtra("DATE", 1);
- }
- intentResult.putExtra(
- "RESULT_DATE",
- iToWhichDateAmIRespondingTo
- );
- setResult(
- RESULT_OK,
- intentResult
- );
- finish();
- }//actionConfirmDate
- void actionCancel(){
- setResult(
- RESULT_CANCELED
- );
- finish();
- }//actionCancel
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.cl_input_date_v1);
- init();
- }//onCreate
- void init(){
- mCaller = getIntent();
- //bindings
- mNpYear = findViewById(R.id.idNpYear);
- mNpMonth = findViewById(R.id.idNpMonth);
- mNpDay = findViewById(R.id.idNpDay);
- //set limits on the NumberPickers
- mNpYear.setMinValue(1);
- mNpYear.setMaxValue(3021);
- mNpMonth.setMinValue(1);
- mNpMonth.setMaxValue(12);
- mNpDay.setMinValue(1);
- mNpDay.setMaxValue(31);
- //TODO - current date
- Calendar c = Calendar.getInstance();
- mNpYear.setValue(c.get(Calendar.YEAR));
- mNpMonth.setValue(c.get(Calendar.MONTH)+1);
- mNpDay.setValue(c.get(Calendar.DATE));
- mBtnConfirmDate = findViewById(R.id.idBtnConfirmDate);
- mBtnCancel = findViewById(R.id.idBtnCancel);
- mBtnConfirmDate.setOnClickListener(mClickHandler);
- mBtnCancel.setOnClickListener(mClickHandler);
- //behaviors
- }//init
- }
Advertisement
Add Comment
Please, Sign In to add comment