Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.activslogs;
- 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.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- //все свойства и атрибуты имеют префикс m -member
- // автоматическое дополнение alt+enter
- private TextView tv;
- private EditText et;
- private TextView mtvRes;
- private ActivityResultLauncher<Intent> mActivityResultLauncher;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.d(Common.LOG_TAG, "MainActivity create");
- setContentView(R.layout.activity_main);
- mtvRes = findViewById(R.id.tvRes);
- mActivityResultLauncher =registerForActivityResult(
- new ActivityResultContracts.StartActivityForResult(),
- new ActivityResultCallback<ActivityResult>() {
- @Override
- public void onActivityResult(ActivityResult result) {
- if (result.getResultCode() == Activity.RESULT_OK){
- Intent intent = result.getData();
- mtvRes.setText(intent.getStringExtra("param"));
- Toast.makeText(getApplicationContext(),
- "Успешно вернулись",
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- );
- ((Button) findViewById(R.id.bRes)).setOnClickListener(v->{
- Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
- intent.putExtra("in param", mtvRes.getText());
- mActivityResultLauncher.launch(intent);
- });
- tv = findViewById(R.id.tv);
- et = findViewById(R.id.etInput);
- tv.setText("Доброе утро");
- View.OnClickListener cl = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- btnClick(view);
- }
- };
- ((Button) findViewById(R.id.button)).setOnClickListener(cl);
- ((TextView) findViewById(R.id.textView4)).setOnClickListener(cl);
- ((Button) findViewById(R.id.btnSecondActivity)).setOnClickListener(v->{
- Intent intent = new Intent(MainActivity.this,SecondActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("param1","1 параметр от основной активности");
- bundle.putString("title", "запущена из основной активности");
- intent.putExtras(bundle);
- //intent.putExtra("param1", "1 параметр от основной активности");
- startActivity(intent);
- });
- }
- @Override
- protected void onStart() {
- super.onStart();
- Log.d(Common.LOG_TAG, "MainActivity start");
- }
- @Override
- protected void onStop() {
- super.onStop();
- Log.d(Common.LOG_TAG, "MainActivity stop");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d(Common.LOG_TAG, "MainActivity destroy");
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.d(Common.LOG_TAG, "MainActivity pause");
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(Common.LOG_TAG, "MainActivity resume");
- }
- public void btnClick(View view) {
- switch (view.getId()) {
- case R.id.button: {
- String s = et.getText().toString();
- if (TextUtils.isEmpty(s)) {
- et.setError("Укажите текст");
- return;
- }
- tv.setText(s);
- break;
- }
- case R.id.textView4: {
- tv.setText("4 февраля. Ура!");
- break;
- }
- }
- tv.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View view) {
- tv.setText("Доброе утро");
- return false;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement