标签:des android style blog http color io os 使用
//MainActivity package cn.eoe.helloeoe; import android.support.v7.app.ActionBarActivity; import android.R.string; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private Button btnStartAty1; private TextView tvOut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartAty1 = (Button) findViewById(R.id.btnStartAty1); tvOut = (TextView) findViewById(R.id.tvOut); btnStartAty1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, Aty1.class); i.putExtra("text", "get from main"); // startActivity(i); startActivityForResult(i, 0); } }); System.out.println("onCreate!!"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getStringExtra("result"); tvOut.setText(result); super.onActivityResult(requestCode, resultCode, data); } @Override protected void onStart() { System.out.println("onStart"); super.onStart(); } @Override protected void onResume() { System.out.println("onResume"); super.onResume(); } @Override protected void onPause() { System.out.println("onPause"); super.onPause(); } @Override protected void onStop() { System.out.println("onStop"); super.onStop(); } @Override protected void onDestroy() { System.out.println("onDestroy"); super.onDestroy(); } @Override protected void onRestart() { System.out.println("onRestart"); super.onRestart(); } }
//son activity public class Aty1 extends Activity { private Button btnClose; private TextView tvOut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aty1); btnClose = (Button) findViewById(R.id.btnClose); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent result = new Intent(); result.putExtra("result", "from Aty1"); setResult(0,result); finish(); } }); tvOut = (TextView) findViewById(R.id.tvOut); String fromMain = getIntent().getStringExtra("text"); tvOut.setText(fromMain); } }
activity声明周期以及自动调用的函数
(1)第一次启动会调用oncreate onstart onresume
(2)被其他activity挡住当前activity会调用onpause
(3)如果activity在onpause后系统需要更多内存,则会杀死activity,并调用onstop;
1、试用了插件后,可在对应的资源文件xml中修改其id号,并使用findViewById()找到插件;
2、监听按钮
btnStartAty1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, Aty1.class); i.putExtra("text", "get from main"); // startActivity(i); startActivityForResult(i, 0); } });
setOnClickListener()要求传入一个OnClickListener的实例,于是直接编写匿名函数,并重写onclick函数;
函数中使用一个intent装载了一个父acitivity和一个子activity的class,并使用startActivity()可打开一个新的activity;
如果使用startActivityForResult()打开一个activity,则可以监听其返回值,用于两个activity之间的数据互通;
3、子activity返回结果
使用setResult()与父activity交换数据,父activity重写
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getStringExtra("result"); tvOut.setText(result); super.onActivityResult(requestCode, resultCode, data); }
接受setresutl的结果,data就是setresult传回来的intent;
4、使用finish()关闭一个activity;
标签:des android style blog http color io os 使用
原文地址:http://www.cnblogs.com/iyjhabc/p/4020930.html