Advertisement
mamamaria

Main Activity

Feb 17th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. package com.example.activslogs;
  2.  
  3. import androidx.activity.result.ActivityResult;
  4. import androidx.activity.result.ActivityResultCallback;
  5. import androidx.activity.result.ActivityResultLauncher;
  6. import androidx.activity.result.contract.ActivityResultContract;
  7. import androidx.activity.result.contract.ActivityResultContracts;
  8. import androidx.appcompat.app.AppCompatActivity;
  9.  
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.os.Bundle;
  13. import android.text.TextUtils;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22. //все свойства и атрибуты имеют префикс m -member
  23. // автоматическое дополнение alt+enter
  24. private TextView tv;
  25. private EditText et;
  26. private TextView mtvRes;
  27. private ActivityResultLauncher<Intent> mActivityResultLauncher;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. Log.d(Common.LOG_TAG, "MainActivity create");
  33. setContentView(R.layout.activity_main);
  34.  
  35. mtvRes = findViewById(R.id.tvRes);
  36.  
  37. mActivityResultLauncher =registerForActivityResult(
  38. new ActivityResultContracts.StartActivityForResult(),
  39. new ActivityResultCallback<ActivityResult>() {
  40. @Override
  41. public void onActivityResult(ActivityResult result) {
  42. if (result.getResultCode() == Activity.RESULT_OK){
  43. Intent intent = result.getData();
  44. mtvRes.setText(intent.getStringExtra("param"));
  45. Toast.makeText(getApplicationContext(),
  46. "Успешно вернулись",
  47. Toast.LENGTH_SHORT).show();
  48. }
  49. }
  50. }
  51. );
  52.  
  53. ((Button) findViewById(R.id.bRes)).setOnClickListener(v->{
  54. Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
  55. intent.putExtra("in param", mtvRes.getText());
  56. mActivityResultLauncher.launch(intent);
  57. });
  58.  
  59.  
  60. tv = findViewById(R.id.tv);
  61. et = findViewById(R.id.etInput);
  62. tv.setText("Доброе утро");
  63.  
  64. View.OnClickListener cl = new View.OnClickListener() {
  65. @Override
  66. public void onClick(View view) {
  67. btnClick(view);
  68. }
  69. };
  70. ((Button) findViewById(R.id.button)).setOnClickListener(cl);
  71. ((TextView) findViewById(R.id.textView4)).setOnClickListener(cl);
  72.  
  73. ((Button) findViewById(R.id.btnSecondActivity)).setOnClickListener(v->{
  74. Intent intent = new Intent(MainActivity.this,SecondActivity.class);
  75. Bundle bundle = new Bundle();
  76. bundle.putString("param1","1 параметр от основной активности");
  77. bundle.putString("title", "запущена из основной активности");
  78. intent.putExtras(bundle);
  79. //intent.putExtra("param1", "1 параметр от основной активности");
  80. startActivity(intent);
  81. });
  82. }
  83.  
  84. @Override
  85. protected void onStart() {
  86. super.onStart();
  87. Log.d(Common.LOG_TAG, "MainActivity start");
  88. }
  89.  
  90. @Override
  91. protected void onStop() {
  92. super.onStop();
  93. Log.d(Common.LOG_TAG, "MainActivity stop");
  94. }
  95.  
  96. @Override
  97. protected void onDestroy() {
  98. super.onDestroy();
  99. Log.d(Common.LOG_TAG, "MainActivity destroy");
  100. }
  101.  
  102. @Override
  103. protected void onPause() {
  104. super.onPause();
  105. Log.d(Common.LOG_TAG, "MainActivity pause");
  106. }
  107.  
  108. @Override
  109. protected void onResume() {
  110. super.onResume();
  111. Log.d(Common.LOG_TAG, "MainActivity resume");
  112. }
  113.  
  114. public void btnClick(View view) {
  115. switch (view.getId()) {
  116. case R.id.button: {
  117. String s = et.getText().toString();
  118. if (TextUtils.isEmpty(s)) {
  119. et.setError("Укажите текст");
  120. return;
  121. }
  122. tv.setText(s);
  123.  
  124. break;
  125. }
  126. case R.id.textView4: {
  127. tv.setText("4 февраля. Ура!");
  128. break;
  129. }
  130. }
  131.  
  132. tv.setOnLongClickListener(new View.OnLongClickListener() {
  133. @Override
  134. public boolean onLongClick(View view) {
  135. tv.setText("Доброе утро");
  136. return false;
  137. }
  138. });
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement