Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <!-- activity_a2.xml -->
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".A2">
- <EditText
- android:gravity="center_horizontal"
- android:hint="yyyy-mm-dd"
- android:id="@+id/idEtResult"
- app:layout_constraintTop_toTopOf="parent"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/idBtnConfirm"
- android:text="@string/strBtnConfirm"
- app:layout_constraintTop_toBottomOf="@id/idEtResult"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- app:layout_constraintTop_toBottomOf="@id/idBtnConfirm"
- android:id="@+id/idBtnCancel"
- android:text="@string/strBtnCancel"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </androidx.constraintlayout.widget.ConstraintLayout>
- *****************************
- <?xml version="1.0" encoding="utf-8"?>
- <!-- activity_main.xml -->
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <LinearLayout
- android:orientation="horizontal"
- app:layout_constraintTop_toTopOf="parent"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:gravity="center_horizontal"
- android:text="@string/strTvResult"
- android:id="@+id/idTvResult"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/idBtnGetResult"
- android:text="@string/strBtnGetResult"
- android:layout_weight="2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </androidx.constraintlayout.widget.ConstraintLayout>
- *******************************
- <!-- strings.xml -->
- <resources>
- <string name="app_name">SAFR</string>
- <string name="strBtnGetResult">get result</string>
- <string name="strTvResult">result will appear here</string>
- <string name="strBtnConfirm">confirm result</string>
- <string name="strBtnCancel">cancel result</string>
- </resources>
- **************+
- //MainActivity.java
- package com.joythis.android.safr;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- public final static int
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY = 4321;
- public final static String KEY_RESPONSE = "KEY_REPONSE";
- Activity mA;
- TextView mTvResult;
- Button mBtnGetResult;
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnGetResult:
- actionGetResult_retro();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void actionGetResult_retro(){
- Intent goA2 = new Intent(
- MainActivity.this,
- A2.class
- );
- //startActivity(goA2);
- //1 - old SAFR
- startActivityForResult(
- goA2,
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY
- );
- }//actionGetResult_retro
- //2 - old SAFR
- /*
- este método será AUTOMATICAMENTE chamado pelo
- sistema Android, quando uma Activity qualquer
- tenha uma resposta para esta Activity em que
- estamos
- */
- @Override
- protected void onActivityResult(
- int requestCode, // para diferenciar entre pedidos
- int resultCode, // OK ou CANCELLED
- @Nullable Intent data // poderá haver dados na resposta
- )
- {
- super.onActivityResult(requestCode, resultCode, data);
- boolean bShouldIRespond = requestCode ==
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY;
- if(bShouldIRespond){
- boolean bConfirmed = resultCode == this.RESULT_OK;
- boolean bCanceled = resultCode == this.RESULT_CANCELED;
- if (bConfirmed){
- // TODO : coisas a fazer com os dados respondidos
- if(data!=null) { // há dados?
- // se sim, aceder por chaves
- String theResponse =
- data.getStringExtra(KEY_RESPONSE);
- mTvResult.setText(theResponse);
- }
- }
- else{
- mTvResult.setText("result canceled");
- }
- }//if
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- //1 - assocs
- mA = this;
- mTvResult = findViewById(R.id.idTvResult);
- mBtnGetResult = findViewById(R.id.idBtnGetResult);
- //2 - behaviors
- mBtnGetResult.setOnClickListener(mClickHandler);
- }//init
- }//MainActivity
- **************************************
- //A2.java
- package com.joythis.android.safr;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class A2 extends AppCompatActivity {
- Activity mA;
- EditText mEtResult;
- Button mBtnConfirm, mBtnCancel;
- View.OnClickListener mClickHandler=new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnConfirm:
- actionConfirm();
- break;
- case R.id.idBtnCancel:
- actionCancel();
- break;
- }//switch
- }
- };//mClickHandler
- void actionConfirm(){
- String theData =
- mEtResult.getText().toString().trim();
- Intent toTransportTheResult = new Intent();
- toTransportTheResult.putExtra(
- MainActivity.KEY_RESPONSE,
- theData
- );
- this.setResult(
- RESULT_OK,
- toTransportTheResult
- );
- finish();
- }//actionConfirm
- void actionCancel(){
- this.setResult(RESULT_CANCELED);
- finish();
- }//actionCancel
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_a2);
- init();
- }//onCreate
- void init(){
- //1 - assocs
- mA = this;
- mEtResult = findViewById(R.id.idEtResult);
- mBtnConfirm = findViewById(R.id.idBtnConfirm);
- mBtnCancel = findViewById(R.id.idBtnCancel);
- //2 - behaviors
- mBtnConfirm.setOnClickListener(mClickHandler);
- mBtnCancel.setOnClickListener(mClickHandler);
- }
- }//A2
- ******
- //MainActivity.java
- package com.joythis.android.safr;
- import androidx.activity.result.ActivityResult;
- import androidx.activity.result.ActivityResultCallback;
- import androidx.activity.result.ActivityResultLauncher;
- import androidx.activity.result.contract.ActivityResultContract;
- import androidx.activity.result.contract.ActivityResultContracts;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityOptionsCompat;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- public final static int
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY = 4321;
- public final static String KEY_RESPONSE = "KEY_REPONSE";
- Activity mA;
- TextView mTvResult;
- Button mBtnGetResult;
- // modern SAFR start
- //1
- ActivityResultContract safrContract = new
- ActivityResultContracts.StartActivityForResult();
- //2
- ActivityResultCallback<ActivityResult> safrCallback =
- new ActivityResultCallback<ActivityResult>() {
- @Override
- public void onActivityResult(ActivityResult result) {
- if(result!=null){
- boolean bCanceled = result.getResultCode()==RESULT_CANCELED;
- boolean bConfirm = result.getResultCode()==RESULT_OK;
- if(bConfirm){
- Intent theData = result.getData();
- if(theData!=null){
- String theResult =
- theData.getStringExtra(KEY_RESPONSE);
- mTvResult.setText(theResult);
- }//if
- }//if bConfirm
- if(bCanceled){
- mTvResult.setText("result canceled");
- }
- }//if
- }//onActivityResult
- };//safrCallback
- //3
- ActivityResultLauncher<Intent> safrLauncher = registerForActivityResult(
- safrContract,
- safrCallback
- );
- // modern SAFR end
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnGetResult:
- //actionGetResult_retro();
- actionGetResult_modern();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void actionGetResult_retro(){
- Intent goA2 = new Intent(
- MainActivity.this,
- A2.class
- );
- //startActivity(goA2);
- //1 - old SAFR
- startActivityForResult(
- goA2,
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY
- );
- }//actionGetResult_retro
- void actionGetResult_modern(){
- Intent goA2 = new Intent(
- MainActivity.this,
- A2.class
- );
- // TODO
- }//actionGetResult_modern
- //2 - old SAFR
- /*
- este método será AUTOMATICAMENTE chamado pelo
- sistema Android, quando uma Activity qualquer
- tenha uma resposta para esta Activity em que
- estamos
- */
- @Override
- protected void onActivityResult(
- int requestCode, // para diferenciar entre pedidos
- int resultCode, // OK ou CANCELLED
- @Nullable Intent data // poderá haver dados na resposta
- )
- {
- super.onActivityResult(requestCode, resultCode, data);
- boolean bShouldIRespond = requestCode ==
- CALL_THIS_NUMBER_WHEN_RESULT_FROM_A2_IS_READY;
- if(bShouldIRespond){
- boolean bConfirmed = resultCode == this.RESULT_OK;
- boolean bCanceled = resultCode == this.RESULT_CANCELED;
- if (bConfirmed){
- // TODO : coisas a fazer com os dados respondidos
- if(data!=null) { // há dados?
- // se sim, aceder por chaves
- String theResponse =
- data.getStringExtra(KEY_RESPONSE);
- mTvResult.setText(theResponse);
- }
- }
- else{
- mTvResult.setText("result canceled");
- }
- }//if
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- //1 - assocs
- mA = this;
- mTvResult = findViewById(R.id.idTvResult);
- mBtnGetResult = findViewById(R.id.idBtnGetResult);
- //2 - behaviors
- mBtnGetResult.setOnClickListener(mClickHandler);
- }//init
- }//MainActivity
- *********************************
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/idLlLabels"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:gravity="center_horizontal"
- android:text="@string/strTVDate1"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:gravity="center_horizontal"
- android:text="@string/strTVDate2"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout
- android:layout_below="@id/idLlLabels"
- android:id="@+id/idLlDates"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:id="@+id/idEtDate1"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/idBtnPickDate1"
- android:text="@string/strBtnPickDate1"
- android:layout_weight="2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:layout_weight="1"
- android:id="@+id/idEtDate2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_weight="2"
- android:id="@+id/idBtnPickDate2"
- android:text="@string/strBtnPickDate2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </LinearLayout>
- <Button
- android:layout_below="@id/idLlDates"
- android:id="@+id/idBtnComputeDistance"
- android:text="@string/strBtnComputeDistance"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <ListView
- android:layout_below="@id/idBtnComputeDistance"
- android:id="@+id/idLvDistances"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
- *********
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:id="@+id/idLlLabels"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_weight="1"
- android:text="@string/strTvYear"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_weight="1"
- android:text="@string/strTvMonth"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_weight="1"
- android:text="@string/strTvDay"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/idLlInputs"
- android:layout_below="@id/idLlLabels"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpYear"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpMonth"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <NumberPicker
- android:layout_weight="1"
- android:id="@+id/idNpDay"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment