一、如何添加新页面
- src->new->other->Android->Android Activity
二、如何跳转页面
通过在Botton函数中添加onclieck函数实现点击跳转。
public void gotoSecondPage(View v)
{
//设置跳转的页面
Intent intent = new Intent(this, SecActivity.class);
//实行跳转
startActivity(intent);
}
三、跳转如何传参
- 方式一
在实行跳转页面前进行put数据,在第二个页面中进行get数据。public void gotoSecondPage(View v) { //设置跳转的页面 Intent intent = new Intent(this, SecActivity.class); intent.putExtra("data", "jiangyo"); //实行跳转 startActivity(intent); }
private String data; @SuppressLint("ShowToast") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sec); Intent i = this.getIntent(); data = i.getStringExtra("data"); Toast.makeText(this, data, 0).show(); }
- 方式二
借助bundle类,可实现多种数据类型的传递。public void gotoSecondPage(View v) { //设置跳转的页面 Intent intent = new Intent(this, SecActivity.class); Bundle bn = new Bundle(); bn.putString("data", "jiangyo"); bn.putInt("ID", 100); intent.putExtras(bn); //实行跳转 startActivity(intent); }
private String data; private int id; @SuppressLint("ShowToast") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sec); Intent i = this.getIntent(); Bundle bn = i.getExtras(); data = bn.getString("data"); id = bn.getInt("ID"); Toast.makeText(this, data+id, 0).show(); }
三、页面自动跳转
- 安卓线程。
- 实现页面的诺干秒后的自动跳转效果。run方法(函数)是线程要做的”事情”,相当linuxC线程的回调函数启动线程。
package com.example.jiangyo.learn; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread t = new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //设置跳转的页面 Intent intent = new Intent(MainActivity.this, SecActivity.class); //实行跳转 startActivity(intent); } }); t.start(); } }
四、Activity(页面)的生命周期