壮心未与年俱老,死去犹能作鬼雄。这篇文章主要讲述Android ActivityIntent相关的知识,希望能为你提供帮助。
首先定义一个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 8protected void onCreate(Bundle savedInstanceState) { 9super.onCreate(savedInstanceState); 10Log.e(this.getClass().getName(), "onCreate"); 11} 12 13@Override 14protected void onStart() { 15super.onStart(); 16Log.e(this.getClass().getName(), "onStart"); 17} 18 19@Override 20protected void onResume() { 21super.onResume(); 22Log.e(this.getClass().getName(), "onResume"); 23} 24 25@Override 26protected void onPause() { 27super.onPause(); 28Log.e(this.getClass().getName(), "onPause"); 29} 30 31@Override 32protected void onStop() { 33super.onStop(); 34Log.e(this.getClass().getName(), "onStop"); 35} 36 37@Override 38protected void onDestroy() { 39super.onDestroy(); 40Log.e(this.getClass().getName(), "onDestroy"); 41} 42 }
打开APP,并启动下一个界面,然后返回主界面
文章图片
点击事件监听
一、自己点击自己
【Android ActivityIntent】
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 11public static final String TAG = "MainActivity"; 12Button button,buttonNext,buttonMsg,buttonSex,buttonHeight,buttonBack; 13 14public static final int SEX_SELECT = 1; 15public static final int HEIGHT_SELECT = 2; 16 17@Override 18protected void onCreate(Bundle savedInstanceState) { 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21 22//自己点击自己 23button = (Button) findViewById(R.id.main_btn); 24button.setOnClickListener(new OnClickListener() { 25@Override 26public void onClick(View v) { 27//定义一个意图,启动FirstActivity 28Intent intent = new Intent(MainActivity.this, MainActivity.class); 29Toast.makeText(MainActivity.this, "你点击了自己", Toast.LENGTH_SHORT).show(); 30startActivity(intent); 31} 32}); 33 34//启动下一个界面,不带参数 35buttonNext = (Button) findViewById(R.id.main_btn_next); 36buttonNext.setOnClickListener(new OnClickListener() { 37@Override 38public void onClick(View v) { 39//定义一个意图,启动FirstActivity 40Intent intent = new Intent(MainActivity.this, FirstActivity.class); 41startActivity(intent); 42} 43}); 44 45//启动下一个界面,并传递参数给下一个界面,比如发送一个句话 46buttonMsg = (Button)findViewById(R.id.main_btn_msg); 47buttonMsg.setOnClickListener(new OnClickListener() { 48@Override 49public void onClick(View v) { 50Intent intent = new Intent(MainActivity.this, SecondActivity.class); 51intent.putExtra("msg", "我要送你一句话"); 52startActivity(intent); 53 54} 55}); 56 57 58//启动下一个界面,选择性别,并将选择结果返回显示在MainActivity中 59buttonSex = (Button)findViewById(R.id.main_sex); 60buttonSex.setOnClickListener(new OnClickListener() { 61@Override 62public void onClick(View v) { 63Intent intent = new Intent(MainActivity.this, ThirdActivity.class); 64 65startActivityForResult(intent, SEX_SELECT); 66} 67}); 68 69//启动下一个界面,输入身高,并将结果返回显示在MainActivity中 70buttonHeight = (Button) findViewById(R.id.main_height); 71buttonHeight.setOnClickListener(new OnClickListener() { 72@Override 73public void onClick(View v) { 74Intent intent = new Intent(MainActivity.this, FourActivity.class); 75startActivityForResult(intent, HEIGHT_SELECT); 76} 77}); 78 79//退出App 80buttonBack = (Button) findViewById(R.id.main_btn_back); 81buttonBack.setOnClickListener(new OnClickListener() { 82@Override 83public void onClick(View v) { 84//主活动点击返回直接退出程序 85finish(); 86} 87}); 88} 89 90 91@Override 92protected void onActivityResult(int requestCode, int resultCode, Intent data) { 93 94switch (requestCode) { 95case SEX_SELECT: 96String sex; 97if(resultCode == ThirdActivity.MAN_CODE){ 98sex = data.getStringExtra("sex_result"); 99Log.e(TAG, "sex = "+sex); //null 100buttonSex.setText("选择了"+sex); 101}else { 102sex = data.getStringExtra("sex_result"); 103buttonSex.setText("选择了"+sex); 104} 105break; 106case HEIGHT_SELECT: 107int height; 108if(resultCode == FourActivity.SUBMIT){ 109//必须给默认值 110height = data.getIntExtra("height", -1); 111buttonHeight.setText(height+"cm"); 112} 113break; 114default: 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 9protected void onCreate(Bundle savedInstanceState) { 10super.onCreate(savedInstanceState); 11setContentView(R.layout.activity_first); 12 13//关闭这个界面,会回到上一个界面 14Button buttonBack = (Button)findViewById(R.id.first_btn_back); 15buttonBack.setOnClickListener(new OnClickListener() { 16@Override 17public void onClick(View v) { 18//返回到上一个界面 19finish(); 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 10TextView tv; 11 12@Override 13protected void onCreate(Bundle savedInstanceState) { 14super.onCreate(savedInstanceState); 15setContentView(R.layout.activity_second); 16tv = (TextView) findViewById(R.id.text); 17 18//获取意图 19Intent intent = getIntent(); 20//通过键key获取数据值 21String data = https://www.songbingjia.com/android/intent.getStringExtra("msg"); 22 23//显示接收到的数据 24tv.setText("收到:"+data); 25 26Button buttonBack = (Button)findViewById(R.id.second_btn_back); 27buttonBack.setOnClickListener(new OnClickListener() { 28@Override 29public void onClick(View v) { 30finish(); 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{ 8public static final int MAN_CODE = 1; 9public static final int WOMAN_CODE = 2; 10 11Button man,woman; 12Intent intent; 13@Override 14protected void onCreate(Bundle savedInstanceState) { 15super.onCreate(savedInstanceState); 16setContentView(R.layout.activity_third); 17 18 19man = (Button) findViewById(R.id.man); 20man.setOnClickListener(this); 21 22woman = (Button) findViewById(R.id.woman); 23woman.setOnClickListener(this); 24 25} 26 27@Override 28public void onClick(View v) { 29intent = new Intent(); 30switch (v.getId()) { 31case R.id.man: 32intent.putExtra("sex_result", "男"); 33 34setResult(MAN_CODE, intent); 35finish(); 36break; 37case R.id.woman: 38intent.putExtra("sex_result", "女"); 39setResult(WOMAN_CODE, intent); 40finish(); 41break; 42default: 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{ 9Button submit,cancel; 10EditText et; 11@Override 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14setContentView(R.layout.activity_four); 15 16submit = (Button) findViewById(R.id.four_submit); 17cancel = (Button) findViewById(R.id.four_cancel); 18et = (EditText) findViewById(R.id.four_edit); 19 20submit.setOnClickListener(this); 21cancel.setOnClickListener(this); 22} 23 24public static final int SUBMIT = 1; 25public static final int CANCEL = 2; 26@Override 27public void onClick(View v) { 28Intent intent = new Intent(); 29switch (v.getId()) { 30case R.id.four_submit: 31int height = Integer.parseInt(et.getText().toString()); 32intent.putExtra("height", height); 33setResult(SUBMIT, intent); 34finish(); 35break; 36case R.id.four_cancel: 37setResult(CANCEL); 38finish(); 39break; 40default: 41break; 42} 43 44} 45 }
推荐阅读
- 不能删除文件或者程序,提示程序被占用
- Eclipse开发Android程序如何在手机上运行
- 小试Android中使用MVC框架模式
- Android 实现高斯模糊效果及低版本兼容
- 当观察者模式和回调机制遇上Android源码
- Android控件使用 — 12个Material Design风格控件的使用
- Android计算app启动时间之二
- Android手机自动化测试实例详解
- android-时间选择器和日期选择器