Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> (一)Activity參數傳遞,activity參數傳遞

(一)Activity參數傳遞,activity參數傳遞

編輯:關於android開發

(一)Activity參數傳遞,activity參數傳遞


1.主Activity,用於啟動另一個Activity()
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate");

findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);
intent.putExtra("data","Hello Henry"); //進行參數傳遞
//startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}

@Override
protected void onStart()
{
super.onStart();
System.out.println("onStrart");
}
@Override
protected void onRestart()
{
super.onRestart();
System.out.println("onRestart");
}
@Override
protected void onStop()
{
super.onStart();
System.out.println("onStop");
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause()
{
super.onPause();
System.out.println("onPause");
}
@Override
protected void onDestroy()
{
super.onDestroy();
System.out.println("onDestroy");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
2.從Activity 被MainActivity啟動
public class BActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
System.out.println("B onCreate");

Intent intent=getIntent(); //獲取MainActivity傳遞過來的Intent

TextView tv= (TextView) findViewById(R.id.tv);
tv.setText(intent.getStringExtra("data")); //調用Intent的getStringExtra("data")方法取出數據
    }

@Override
protected void onStart()
{
super.onStart();
System.out.println("B onStrart");
}
@Override
protected void onRestart()
{
super.onRestart();
System.out.println("B onRestart");
}
@Override
protected void onStop()
{
super.onStart();
System.out.println("B onStop");
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("B onResume");
}
@Override
protected void onPause()
{
super.onPause();
System.out.println("B onPause");
}
@Override
protected void onDestroy()
{
super.onDestroy();
System.out.println("B onDestroy");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_b, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
3.傳遞數據包Bundle
3.1 MainActivity中的

findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
        @Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);
//intent.putExtra("data","Hello Henry");

//傳遞數據包
Bundle bundle=new Bundle();
bundle.putString("name","henry"); //往數據包中添加數據
bundle.putInt("age",24);
intent.putExtras(bundle); //將數據包添加到Intent中

//startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}
3.2 BActivity中的
Intent intent=getIntent();
Bundle bundle=intent.getExtras(); //接收數據包

TextView tv= (TextView) findViewById(R.id.tv);
//tv.setText(intent.getStringExtra("data"));
tv.setText(String.format("name=%s,age=%d",bundle.getString("name"),(int)bundle.get("age"))); //調用Bundle的getXXX()函數獲取數據
4.傳遞引用
4.1使用序列化的方式傳遞引用類
自定義類:
public class User implements Serializable {        //使用java提供的序列化的方式   
private String name;
private int age;

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
public User(String name,int age)
{
this.name=name;
this.age=age;
}
}
主Activity:
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);


intent.putExtra("user", (Serializable) new User("henryQL",24)); //添加自定義的引用類的對象
startActivity(intent);
}
});
}

}
被調用的Activity:
public class BActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);

Intent intent=getIntent();
User user= (User) intent.getSerializableExtra("user"); //獲取序列化的數據

TextView tv= (TextView) findViewById(R.id.tv);
tv.setText(String.format("nmae=%s,age=%d",user.getName(),user.getAge()));
}
}
4.2 使用Android提供的Parcelable接口
public class User implements Parcelable {
private String name;
private int age;

public void setName(String name) {
this.name = name;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
public User(String name,int age)
{
this.name=name;
this.age=age;
}


@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(getName());
parcel.writeInt(getAge());
}

public static final Parcelable.Creator<User> CREATOR=new Parcelable.Creator<User>() //必須命名為CREATOR
{

@Override
public User createFromParcel(Parcel parcel) {
return new User(parcel.readString(),parcel.readInt());
}

@Override
public User[] newArray(int i) {
return new User[i];
}
};
}

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate");

findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);

intent.putExtra("user",new User("Qianlong",24));

//startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}
}

public class BActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
System.out.println("B onCreate");


Intent intent=getIntent();
User user=intent.getParcelableExtra("user"); //獲取Parcelable對象

TextView tv= (TextView) findViewById(R.id.tv);
//tv.setText(intent.getStringExtra("data"));
//tv.setText(String.format("name=%s,age=%d",bundle.getString("name"),(int)bundle.get("age")));
tv.setText(String.format("nmae=%s,age=%d",user.getName(),user.getAge()));
}
}
 




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved