am_dot_com

DDM 2022-11-29

Nov 29th, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.62 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- activity_a2.xml -->
  3. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".A2">
  9.  
  10. <EditText
  11. android:gravity="center_horizontal"
  12. android:hint="yyyy-mm-dd"
  13. android:id="@+id/idEtResult"
  14. app:layout_constraintTop_toTopOf="parent"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"/>
  17.  
  18. <Button
  19. android:id="@+id/idBtnConfirm"
  20. android:text="@string/strBtnConfirm"
  21. app:layout_constraintTop_toBottomOf="@id/idEtResult"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"/>
  24.  
  25. <Button
  26. app:layout_constraintTop_toBottomOf="@id/idBtnConfirm"
  27. android:id="@+id/idBtnCancel"
  28. android:text="@string/strBtnCancel"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"/>
  31.  
  32. </androidx.constraintlayout.widget.ConstraintLayout>
  33.  
  34. *****************************
  35.  
  36. <?xml version="1.0" encoding="utf-8"?>
  37. <!-- activity_main.xml -->
  38. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  39. xmlns:app="http://schemas.android.com/apk/res-auto"
  40. xmlns:tools="http://schemas.android.com/tools"
  41. android:layout_width="match_parent"
  42. android:layout_height="match_parent"
  43. tools:context=".MainActivity">
  44.  
  45. <LinearLayout
  46. android:orientation="horizontal"
  47. app:layout_constraintTop_toTopOf="parent"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content">
  50.  
  51. <TextView
  52. android:gravity="center_horizontal"
  53. android:text="@string/strTvResult"
  54. android:id="@+id/idTvResult"
  55. android:layout_weight="1"
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"/>
  58.  
  59. <Button
  60. android:id="@+id/idBtnGetResult"
  61. android:text="@string/strBtnGetResult"
  62. android:layout_weight="2"
  63. android:layout_width="match_parent"
  64. android:layout_height="wrap_content"/>
  65. </LinearLayout>
  66.  
  67. </androidx.constraintlayout.widget.ConstraintLayout>
  68.  
  69. *******************************
  70.  
  71.  
  72. <!-- strings.xml -->
  73. <resources>
  74. <string name="app_name">SAFR</string>
  75. <string name="strBtnGetResult">get result</string>
  76. <string name="strTvResult">result will appear here</string>
  77. <string name="strBtnConfirm">confirm result</string>
  78. <string name="strBtnCancel">cancel result</string>
  79. </resources>
  80.  
  81. **************+
  82.  
  83. //MainActivity.java
  84. package com.joythis.android.safr;
  85.  
  86. import androidx.annotation.Nullable;
  87. import androidx.appcompat.app.AppCompatActivity;
  88.  
  89. import android.app.Activity;
  90. import android.content.Intent;
  91. import android.os.Bundle;
  92. import android.view.View;
  93. import android.widget.Button;
  94. import android.widget.TextView;
  95.  
  96. public class MainActivity extends AppCompatActivity {
  97.  
  98. public final static int
  99. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY = 4321;
  100.  
  101. public final static String KEY_RESPONSE = "KEY_REPONSE";
  102.  
  103. Activity mA;
  104. TextView mTvResult;
  105. Button mBtnGetResult;
  106.  
  107. View.OnClickListener mClickHandler = new View.OnClickListener() {
  108. @Override
  109. public void onClick(View v) {
  110. switch(v.getId()){
  111. case R.id.idBtnGetResult:
  112. actionGetResult_retro();
  113. break;
  114. }//switch
  115. }//onClick
  116. };//mClickHandler
  117.  
  118. void actionGetResult_retro(){
  119. Intent goA2 = new Intent(
  120. MainActivity.this,
  121. A2.class
  122. );
  123. //startActivity(goA2);
  124. //1 - old SAFR
  125. startActivityForResult(
  126. goA2,
  127. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY
  128. );
  129. }//actionGetResult_retro
  130.  
  131. //2 - old SAFR
  132. /*
  133. este método será AUTOMATICAMENTE chamado pelo
  134. sistema Android, quando uma Activity qualquer
  135. tenha uma resposta para esta Activity em que
  136. estamos
  137. */
  138. @Override
  139. protected void onActivityResult(
  140. int requestCode, // para diferenciar entre pedidos
  141. int resultCode, // OK ou CANCELLED
  142. @Nullable Intent data // poderá haver dados na resposta
  143. )
  144. {
  145. super.onActivityResult(requestCode, resultCode, data);
  146.  
  147. boolean bShouldIRespond = requestCode ==
  148. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY;
  149. if(bShouldIRespond){
  150. boolean bConfirmed = resultCode == this.RESULT_OK;
  151. boolean bCanceled = resultCode == this.RESULT_CANCELED;
  152. if (bConfirmed){
  153. // TODO : coisas a fazer com os dados respondidos
  154. if(data!=null) { // há dados?
  155. // se sim, aceder por chaves
  156. String theResponse =
  157. data.getStringExtra(KEY_RESPONSE);
  158.  
  159. mTvResult.setText(theResponse);
  160. }
  161. }
  162. else{
  163. mTvResult.setText("result canceled");
  164. }
  165. }//if
  166. }
  167.  
  168. @Override
  169. protected void onCreate(Bundle savedInstanceState) {
  170. super.onCreate(savedInstanceState);
  171. setContentView(R.layout.activity_main);
  172.  
  173. init();
  174. }//onCreate
  175.  
  176. void init(){
  177. //1 - assocs
  178. mA = this;
  179. mTvResult = findViewById(R.id.idTvResult);
  180. mBtnGetResult = findViewById(R.id.idBtnGetResult);
  181.  
  182. //2 - behaviors
  183. mBtnGetResult.setOnClickListener(mClickHandler);
  184. }//init
  185. }//MainActivity
  186.  
  187. **************************************
  188.  
  189. //A2.java
  190. package com.joythis.android.safr;
  191.  
  192. import androidx.appcompat.app.AppCompatActivity;
  193.  
  194. import android.app.Activity;
  195. import android.content.Intent;
  196. import android.os.Bundle;
  197. import android.view.View;
  198. import android.widget.Button;
  199. import android.widget.EditText;
  200.  
  201. public class A2 extends AppCompatActivity {
  202. Activity mA;
  203. EditText mEtResult;
  204. Button mBtnConfirm, mBtnCancel;
  205.  
  206. View.OnClickListener mClickHandler=new View.OnClickListener() {
  207. @Override
  208. public void onClick(View v) {
  209. switch(v.getId()){
  210. case R.id.idBtnConfirm:
  211. actionConfirm();
  212. break;
  213. case R.id.idBtnCancel:
  214. actionCancel();
  215. break;
  216. }//switch
  217. }
  218. };//mClickHandler
  219.  
  220. void actionConfirm(){
  221. String theData =
  222. mEtResult.getText().toString().trim();
  223.  
  224. Intent toTransportTheResult = new Intent();
  225.  
  226. toTransportTheResult.putExtra(
  227. MainActivity.KEY_RESPONSE,
  228. theData
  229. );
  230.  
  231. this.setResult(
  232. RESULT_OK,
  233. toTransportTheResult
  234. );
  235. finish();
  236. }//actionConfirm
  237.  
  238.  
  239. void actionCancel(){
  240. this.setResult(RESULT_CANCELED);
  241. finish();
  242. }//actionCancel
  243.  
  244. @Override
  245. protected void onCreate(Bundle savedInstanceState) {
  246. super.onCreate(savedInstanceState);
  247. setContentView(R.layout.activity_a2);
  248. init();
  249. }//onCreate
  250.  
  251. void init(){
  252. //1 - assocs
  253. mA = this;
  254. mEtResult = findViewById(R.id.idEtResult);
  255. mBtnConfirm = findViewById(R.id.idBtnConfirm);
  256. mBtnCancel = findViewById(R.id.idBtnCancel);
  257.  
  258. //2 - behaviors
  259. mBtnConfirm.setOnClickListener(mClickHandler);
  260. mBtnCancel.setOnClickListener(mClickHandler);
  261. }
  262. }//A2
  263.  
  264.  
  265. ******
  266.  
  267.  
  268. //MainActivity.java
  269. package com.joythis.android.safr;
  270.  
  271. import androidx.activity.result.ActivityResult;
  272. import androidx.activity.result.ActivityResultCallback;
  273. import androidx.activity.result.ActivityResultLauncher;
  274. import androidx.activity.result.contract.ActivityResultContract;
  275. import androidx.activity.result.contract.ActivityResultContracts;
  276. import androidx.annotation.NonNull;
  277. import androidx.annotation.Nullable;
  278. import androidx.appcompat.app.AppCompatActivity;
  279. import androidx.core.app.ActivityOptionsCompat;
  280.  
  281. import android.app.Activity;
  282. import android.content.Intent;
  283. import android.os.Bundle;
  284. import android.view.View;
  285. import android.widget.Button;
  286. import android.widget.TextView;
  287.  
  288. public class MainActivity extends AppCompatActivity {
  289.  
  290. public final static int
  291. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY = 4321;
  292.  
  293. public final static String KEY_RESPONSE = "KEY_REPONSE";
  294.  
  295. Activity mA;
  296. TextView mTvResult;
  297. Button mBtnGetResult;
  298.  
  299. // modern SAFR start
  300. //1
  301. ActivityResultContract safrContract = new
  302. ActivityResultContracts.StartActivityForResult();
  303.  
  304. //2
  305. ActivityResultCallback<ActivityResult> safrCallback =
  306. new ActivityResultCallback<ActivityResult>() {
  307. @Override
  308. public void onActivityResult(ActivityResult result) {
  309. if(result!=null){
  310. boolean bCanceled = result.getResultCode()==RESULT_CANCELED;
  311. boolean bConfirm = result.getResultCode()==RESULT_OK;
  312. if(bConfirm){
  313. Intent theData = result.getData();
  314. if(theData!=null){
  315. String theResult =
  316. theData.getStringExtra(KEY_RESPONSE);
  317.  
  318. mTvResult.setText(theResult);
  319. }//if
  320. }//if bConfirm
  321. if(bCanceled){
  322. mTvResult.setText("result canceled");
  323. }
  324. }//if
  325. }//onActivityResult
  326. };//safrCallback
  327.  
  328. //3
  329. ActivityResultLauncher<Intent> safrLauncher = registerForActivityResult(
  330. safrContract,
  331. safrCallback
  332. );
  333. // modern SAFR end
  334.  
  335.  
  336. View.OnClickListener mClickHandler = new View.OnClickListener() {
  337. @Override
  338. public void onClick(View v) {
  339. switch(v.getId()){
  340. case R.id.idBtnGetResult:
  341. //actionGetResult_retro();
  342. actionGetResult_modern();
  343. break;
  344. }//switch
  345. }//onClick
  346. };//mClickHandler
  347.  
  348. void actionGetResult_retro(){
  349. Intent goA2 = new Intent(
  350. MainActivity.this,
  351. A2.class
  352. );
  353. //startActivity(goA2);
  354. //1 - old SAFR
  355. startActivityForResult(
  356. goA2,
  357. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY
  358. );
  359. }//actionGetResult_retro
  360.  
  361. void actionGetResult_modern(){
  362. Intent goA2 = new Intent(
  363. MainActivity.this,
  364. A2.class
  365. );
  366. // TODO
  367. }//actionGetResult_modern
  368.  
  369. //2 - old SAFR
  370. /*
  371. este método será AUTOMATICAMENTE chamado pelo
  372. sistema Android, quando uma Activity qualquer
  373. tenha uma resposta para esta Activity em que
  374. estamos
  375. */
  376. @Override
  377. protected void onActivityResult(
  378. int requestCode, // para diferenciar entre pedidos
  379. int resultCode, // OK ou CANCELLED
  380. @Nullable Intent data // poderá haver dados na resposta
  381. )
  382. {
  383. super.onActivityResult(requestCode, resultCode, data);
  384.  
  385. boolean bShouldIRespond = requestCode ==
  386. CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY;
  387. if(bShouldIRespond){
  388. boolean bConfirmed = resultCode == this.RESULT_OK;
  389. boolean bCanceled = resultCode == this.RESULT_CANCELED;
  390. if (bConfirmed){
  391. // TODO : coisas a fazer com os dados respondidos
  392. if(data!=null) { // há dados?
  393. // se sim, aceder por chaves
  394. String theResponse =
  395. data.getStringExtra(KEY_RESPONSE);
  396.  
  397. mTvResult.setText(theResponse);
  398. }
  399. }
  400. else{
  401. mTvResult.setText("result canceled");
  402. }
  403. }//if
  404. }
  405.  
  406. @Override
  407. protected void onCreate(Bundle savedInstanceState) {
  408. super.onCreate(savedInstanceState);
  409. setContentView(R.layout.activity_main);
  410.  
  411. init();
  412. }//onCreate
  413.  
  414. void init(){
  415. //1 - assocs
  416. mA = this;
  417. mTvResult = findViewById(R.id.idTvResult);
  418. mBtnGetResult = findViewById(R.id.idBtnGetResult);
  419.  
  420. //2 - behaviors
  421. mBtnGetResult.setOnClickListener(mClickHandler);
  422. }//init
  423. }//MainActivity
  424. *********************************
  425. <?xml version="1.0" encoding="utf-8"?>
  426. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  427. android:layout_width="match_parent"
  428. android:layout_height="match_parent">
  429.  
  430. <LinearLayout
  431. android:id="@+id/idLlLabels"
  432. android:orientation="horizontal"
  433. android:layout_width="match_parent"
  434. android:layout_height="wrap_content">
  435. <TextView
  436. android:gravity="center_horizontal"
  437. android:text="@string/strTVDate1"
  438. android:layout_weight="1"
  439. android:layout_width="match_parent"
  440. android:layout_height="wrap_content"/>
  441. <TextView
  442. android:gravity="center_horizontal"
  443. android:text="@string/strTVDate2"
  444. android:layout_weight="1"
  445. android:layout_width="match_parent"
  446. android:layout_height="wrap_content"/>
  447. </LinearLayout>
  448.  
  449. <LinearLayout
  450. android:layout_below="@id/idLlLabels"
  451. android:id="@+id/idLlDates"
  452. android:orientation="horizontal"
  453. android:layout_width="match_parent"
  454. android:layout_height="wrap_content">
  455. <LinearLayout
  456. android:orientation="horizontal"
  457. android:layout_weight="1"
  458. android:layout_width="match_parent"
  459. android:layout_height="wrap_content">
  460. <EditText
  461. android:id="@+id/idEtDate1"
  462. android:layout_weight="1"
  463. android:layout_width="match_parent"
  464. android:layout_height="wrap_content"/>
  465. <Button
  466. android:id="@+id/idBtnPickDate1"
  467. android:text="@string/strBtnPickDate1"
  468. android:layout_weight="2"
  469. android:layout_width="match_parent"
  470. android:layout_height="wrap_content"/>
  471. </LinearLayout>
  472. <LinearLayout
  473. android:orientation="horizontal"
  474. android:layout_weight="1"
  475. android:layout_width="match_parent"
  476. android:layout_height="wrap_content">
  477. <EditText
  478. android:layout_weight="1"
  479. android:id="@+id/idEtDate2"
  480. android:layout_width="match_parent"
  481. android:layout_height="wrap_content"/>
  482. <Button
  483. android:layout_weight="2"
  484. android:id="@+id/idBtnPickDate2"
  485. android:text="@string/strBtnPickDate2"
  486. android:layout_width="match_parent"
  487. android:layout_height="wrap_content"/>
  488. </LinearLayout>
  489. </LinearLayout>
  490.  
  491. <Button
  492. android:layout_below="@id/idLlDates"
  493. android:id="@+id/idBtnComputeDistance"
  494. android:text="@string/strBtnComputeDistance"
  495. android:layout_width="match_parent"
  496. android:layout_height="wrap_content"/>
  497.  
  498. <ListView
  499. android:layout_below="@id/idBtnComputeDistance"
  500. android:id="@+id/idLvDistances"
  501. android:layout_width="match_parent"
  502. android:layout_height="wrap_content"/>
  503. </RelativeLayout>
  504.  
  505. *********
  506.  
  507. <?xml version="1.0" encoding="utf-8"?>
  508. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  509. android:layout_width="match_parent"
  510. android:layout_height="match_parent">
  511.  
  512. <LinearLayout
  513. android:id="@+id/idLlLabels"
  514. android:orientation="horizontal"
  515. android:layout_width="match_parent"
  516. android:layout_height="wrap_content">
  517.  
  518. <TextView
  519. android:layout_weight="1"
  520. android:text="@string/strTvYear"
  521. android:layout_width="match_parent"
  522. android:layout_height="wrap_content"/>
  523. <TextView
  524. android:layout_weight="1"
  525. android:text="@string/strTvMonth"
  526. android:layout_width="match_parent"
  527. android:layout_height="wrap_content"/>
  528. <TextView
  529. android:layout_weight="1"
  530. android:text="@string/strTvDay"
  531. android:layout_width="match_parent"
  532. android:layout_height="wrap_content"/>
  533. </LinearLayout>
  534.  
  535. <LinearLayout
  536. android:id="@+id/idLlInputs"
  537. android:layout_below="@id/idLlLabels"
  538. android:orientation="horizontal"
  539. android:layout_width="match_parent"
  540. android:layout_height="wrap_content">
  541. <NumberPicker
  542. android:layout_weight="1"
  543. android:id="@+id/idNpYear"
  544. android:layout_width="match_parent"
  545. android:layout_height="wrap_content"/>
  546. <NumberPicker
  547. android:layout_weight="1"
  548. android:id="@+id/idNpMonth"
  549. android:layout_width="match_parent"
  550. android:layout_height="wrap_content"/>
  551. <NumberPicker
  552. android:layout_weight="1"
  553. android:id="@+id/idNpDay"
  554. android:layout_width="match_parent"
  555. android:layout_height="wrap_content"/>
  556. </LinearLayout>
  557.  
  558. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment