Android Activity Intent
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Activity Intent相关的知识,希望对你有一定的参考价值。
首先定义一个LifeCycleActivity的类,继承Activity,重写onCreat() onStart() onResume() onPause() onStop() onDestroy()
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.util.Log; 4 5 public class LifeCycleActivity extends Activity{ 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 Log.e(this.getClass().getName(), "onCreate"); 11 } 12 13 @Override 14 protected void onStart() { 15 super.onStart(); 16 Log.e(this.getClass().getName(), "onStart"); 17 } 18 19 @Override 20 protected void onResume() { 21 super.onResume(); 22 Log.e(this.getClass().getName(), "onResume"); 23 } 24 25 @Override 26 protected void onPause() { 27 super.onPause(); 28 Log.e(this.getClass().getName(), "onPause"); 29 } 30 31 @Override 32 protected void onStop() { 33 super.onStop(); 34 Log.e(this.getClass().getName(), "onStop"); 35 } 36 37 @Override 38 protected void onDestroy() { 39 super.onDestroy(); 40 Log.e(this.getClass().getName(), "onDestroy"); 41 } 42 }
打开APP,并启动下一个界面,然后返回主界面
点击事件监听
一、自己点击自己
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.util.Log; 4 import android.view.View; 5 import android.view.View.OnClickListener; 6 import android.widget.Button; 7 import android.widget.Toast; 8 9 public class MainActivity extends LifeCycleActivity { 10 11 public static final String TAG = "MainActivity"; 12 Button button,buttonNext,buttonMsg,buttonSex,buttonHeight,buttonBack; 13 14 public static final int SEX_SELECT = 1; 15 public static final int HEIGHT_SELECT = 2; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 //自己点击自己 23 button = (Button) findViewById(R.id.main_btn); 24 button.setOnClickListener(new OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 //定义一个意图,启动FirstActivity 28 Intent intent = new Intent(MainActivity.this, MainActivity.class); 29 Toast.makeText(MainActivity.this, "你点击了自己", Toast.LENGTH_SHORT).show(); 30 startActivity(intent); 31 } 32 }); 33 34 //启动下一个界面,不带参数 35 buttonNext = (Button) findViewById(R.id.main_btn_next); 36 buttonNext.setOnClickListener(new OnClickListener() { 37 @Override 38 public void onClick(View v) { 39 //定义一个意图,启动FirstActivity 40 Intent intent = new Intent(MainActivity.this, FirstActivity.class); 41 startActivity(intent); 42 } 43 }); 44 45 //启动下一个界面,并传递参数给下一个界面,比如发送一个句话 46 buttonMsg = (Button)findViewById(R.id.main_btn_msg); 47 buttonMsg.setOnClickListener(new OnClickListener() { 48 @Override 49 public void onClick(View v) { 50 Intent intent = new Intent(MainActivity.this, SecondActivity.class); 51 intent.putExtra("msg", "我要送你一句话"); 52 startActivity(intent); 53 54 } 55 }); 56 57 58 //启动下一个界面,选择性别,并将选择结果返回显示在MainActivity中 59 buttonSex = (Button)findViewById(R.id.main_sex); 60 buttonSex.setOnClickListener(new OnClickListener() { 61 @Override 62 public void onClick(View v) { 63 Intent intent = new Intent(MainActivity.this, ThirdActivity.class); 64 65 startActivityForResult(intent, SEX_SELECT); 66 } 67 }); 68 69 //启动下一个界面,输入身高,并将结果返回显示在MainActivity中 70 buttonHeight = (Button) findViewById(R.id.main_height); 71 buttonHeight.setOnClickListener(new OnClickListener() { 72 @Override 73 public void onClick(View v) { 74 Intent intent = new Intent(MainActivity.this, FourActivity.class); 75 startActivityForResult(intent, HEIGHT_SELECT); 76 } 77 }); 78 79 //退出App 80 buttonBack = (Button) findViewById(R.id.main_btn_back); 81 buttonBack.setOnClickListener(new OnClickListener() { 82 @Override 83 public void onClick(View v) { 84 //主活动点击返回直接退出程序 85 finish(); 86 } 87 }); 88 } 89 90 91 @Override 92 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 93 94 switch (requestCode) { 95 case SEX_SELECT: 96 String sex; 97 if(resultCode == ThirdActivity.MAN_CODE){ 98 sex = data.getStringExtra("sex_result"); 99 Log.e(TAG, "sex = "+sex);//null 100 buttonSex.setText("选择了"+sex); 101 }else { 102 sex = data.getStringExtra("sex_result"); 103 buttonSex.setText("选择了"+sex); 104 } 105 break; 106 case HEIGHT_SELECT: 107 int height; 108 if(resultCode == FourActivity.SUBMIT){ 109 //必须给默认值 110 height = data.getIntExtra("height", -1); 111 buttonHeight.setText(height+"cm"); 112 } 113 break; 114 default: 115 } 116 } 117 118 }
启动下一个界面,无数据传递,无返回数据,
1 import android.os.Bundle; 2 import android.view.View; 3 import android.view.View.OnClickListener; 4 import android.widget.Button; 5 6 public class FirstActivity extends LifeCycleActivity{ 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_first); 12 13 //关闭这个界面,会回到上一个界面 14 Button buttonBack = (Button)findViewById(R.id.first_btn_back); 15 buttonBack.setOnClickListener(new OnClickListener() { 16 @Override 17 public void onClick(View v) { 18 //返回到上一个界面 19 finish(); 20 } 21 }); 22 23 } 26 }
启动下一个界面,并传递参数给下一个界面,比如发送一个句话
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.Button; 6 import android.widget.TextView; 7 8 public class SecondActivity extends LifeCycleActivity { 9 10 TextView tv; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_second); 16 tv = (TextView) findViewById(R.id.text); 17 18 //获取意图 19 Intent intent = getIntent(); 20 //通过键key获取数据值 21 String data = intent.getStringExtra("msg"); 22 23 //显示接收到的数据 24 tv.setText("收到:"+data); 25 26 Button buttonBack = (Button)findViewById(R.id.second_btn_back); 27 buttonBack.setOnClickListener(new OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 finish(); 31 } 32 }); 33 } 34 35 36 }
启动下一个界面,选择性别,并将选择结果返回显示在MainActivity中
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.Button; 6 7 public class ThirdActivity extends LifeCycleActivity implements OnClickListener{ 8 public static final int MAN_CODE = 1; 9 public static final int WOMAN_CODE = 2; 10 11 Button man,woman; 12 Intent intent; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_third); 17 18 19 man = (Button) findViewById(R.id.man); 20 man.setOnClickListener(this); 21 22 woman = (Button) findViewById(R.id.woman); 23 woman.setOnClickListener(this); 24 25 } 26 27 @Override 28 public void onClick(View v) { 29 intent = new Intent(); 30 switch (v.getId()) { 31 case R.id.man: 32 intent.putExtra("sex_result", "男"); 33 34 setResult(MAN_CODE, intent); 35 finish(); 36 break; 37 case R.id.woman: 38 intent.putExtra("sex_result", "女"); 39 setResult(WOMAN_CODE, intent); 40 finish(); 41 break; 42 default: 43 } 44 } 45 }
//启动下一个界面,输入身高,并将结果返回显示在MainActivity中
1 import android.content.Intent; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.Button; 6 import android.widget.EditText; 7 8 public class FourActivity extends LifeCycleActivity implements OnClickListener{ 9 Button submit,cancel; 10 EditText et; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_four); 15 16 submit = (Button) findViewById(R.id.four_submit); 17 cancel = (Button) findViewById(R.id.four_cancel); 18 et = (EditText) findViewById(R.id.four_edit); 19 20 submit.setOnClickListener(this); 21 cancel.setOnClickListener(this); 22 } 23 24 public static final int SUBMIT = 1; 25 public static final int CANCEL = 2; 26 @Override 27 public void onClick(View v) { 28 Intent intent = new Intent(); 29 switch (v.getId()) { 30 case R.id.four_submit: 31 int height = Integer.parseInt(et.getText().toString()); 32 intent.putExtra("height", height); 33 setResult(SUBMIT, intent); 34 finish(); 35 break; 36 case R.id.four_cancel: 37 setResult(CANCEL); 38 finish(); 39 break; 40 default: 41 break; 42 } 43 44 } 45 }
以上是关于Android Activity Intent的主要内容,如果未能解决你的问题,请参考以下文章
Android12.2 利用Intent启动和关闭Activity
Android基础利用Intent在Activity之间传递数据