am_dot_com

DDM 2021-11-23

Nov 23rd, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package com.joythis.android.distanceindays;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.NumberPicker;
  10.  
  11. import java.util.Calendar;
  12.  
  13. public class InputDateActivity extends AppCompatActivity {
  14. Intent mCaller;
  15. NumberPicker mNpYear, mNpMonth, mNpDay;
  16. Button mBtnConfirmDate, mBtnCancel;
  17.  
  18. View.OnClickListener mClickHandler = new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. switch(v.getId()){
  22. case R.id.idBtnConfirmDate:
  23. actionConfirmDate();
  24. break;
  25. case R.id.idBtnCancel:
  26. actionCancel();
  27. break;
  28. }//switch
  29. }//onClick
  30. };//mClickHandler
  31.  
  32. void actionConfirmDate(){
  33. Intent intentResult = new Intent(
  34. InputDateActivity.this,
  35. MainActivity.class
  36. );
  37. int y,m,d;
  38. y = mNpYear.getValue();
  39. m = mNpMonth.getValue();
  40. d = mNpDay.getValue();
  41.  
  42. String strDate = String.format(
  43. "%d-%d-%d", y,m,d
  44. );
  45. //like this
  46. intentResult.putExtra("DATE", strDate);
  47.  
  48. //or like this
  49. intentResult.putExtra("RESULT_YEAR", y);
  50. intentResult.putExtra("RESULT_MONTH", m);
  51. intentResult.putExtra("RESULT_DAY", d);
  52.  
  53. int iToWhichDateAmIRespondingTo = 1;
  54. if (mCaller!=null){
  55. iToWhichDateAmIRespondingTo =
  56. mCaller.getIntExtra("DATE", 1);
  57. }
  58.  
  59. intentResult.putExtra(
  60. "RESULT_DATE",
  61. iToWhichDateAmIRespondingTo
  62. );
  63.  
  64. setResult(
  65. RESULT_OK,
  66. intentResult
  67. );
  68. finish();
  69. }//actionConfirmDate
  70.  
  71. void actionCancel(){
  72. setResult(
  73. RESULT_CANCELED
  74. );
  75. finish();
  76. }//actionCancel
  77.  
  78. @Override
  79. protected void onCreate(Bundle savedInstanceState) {
  80. super.onCreate(savedInstanceState);
  81. setContentView(R.layout.cl_input_date_v1);
  82.  
  83. init();
  84. }//onCreate
  85.  
  86. void init(){
  87. mCaller = getIntent();
  88. //bindings
  89. mNpYear = findViewById(R.id.idNpYear);
  90. mNpMonth = findViewById(R.id.idNpMonth);
  91. mNpDay = findViewById(R.id.idNpDay);
  92.  
  93. //set limits on the NumberPickers
  94. mNpYear.setMinValue(1);
  95. mNpYear.setMaxValue(3021);
  96.  
  97. mNpMonth.setMinValue(1);
  98. mNpMonth.setMaxValue(12);
  99.  
  100. mNpDay.setMinValue(1);
  101. mNpDay.setMaxValue(31);
  102.  
  103. //TODO - current date
  104. Calendar c = Calendar.getInstance();
  105. mNpYear.setValue(c.get(Calendar.YEAR));
  106. mNpMonth.setValue(c.get(Calendar.MONTH)+1);
  107. mNpDay.setValue(c.get(Calendar.DATE));
  108.  
  109. mBtnConfirmDate = findViewById(R.id.idBtnConfirmDate);
  110. mBtnCancel = findViewById(R.id.idBtnCancel);
  111.  
  112. mBtnConfirmDate.setOnClickListener(mClickHandler);
  113. mBtnCancel.setOnClickListener(mClickHandler);
  114.  
  115. //behaviors
  116. }//init
  117. }
Advertisement
Add Comment
Please, Sign In to add comment