activity的傳值方式
一、初步涉及
- 簡單傳值方式
發送
Intent intent = new Intent(MainActivity.this,Two_ac.class);
intent.putExtra("name", "蓋倫");
intent.putExtra("pass", "4444");
startActivity(intent);
接收
String name = intent.getStringExtra("name");
int pass = intent.getIntExtra("pass", 0);
tv_two.setText(name +"——"+ pass);
這裏還有一種其他寫法
發送
Intent intent = new Intent(MainActivity.this, Two_ac.class);
Bundle bundle = new Bundle();
bundle.putString("name", "提莫");
bundle.putInt("age", 22);
intent.putExtras(bundle);
startActivity(intent);
接收
String name = intent.getStringExtra("name");
int pass = intent.getIntExtra("age", 0);
tv_two.setText(name +"——"+ age);
Intent在不同的組件中傳遞對象數據的方法。
2. 繼承Serializable
3. 繼承Parcelable
時間關係這裏就不貼代碼了,直接下載源碼看吧。
關係兩者的關係可以查看我的博客
Android Parcelable和Serializable的區別(一)、(二)、(三)
Url:http://blog.csdn.net/zhan1350441670/article/details/47274927
二、實例內容
下面就來看看這個具體的實例吧。
首先來看下這個APP的圖片
這裏的話我們可以看到,一些在首頁顯示的文字和圖片當你跳轉到具體內容的頁面時,首頁的文字和圖片傳到了具體內容頁面上。
主頁:
package com.example.activity_two;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
private TextView tv;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.bt_b);
tv = (TextView) findViewById(R.id.tv);
tv.setText("中國面臨大漢以來的最大機遇");
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.title);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Two_ac.class);
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.title);
Bundle bundle = new Bundle();
bundle.putString("title", "中國面臨大漢以來的最大機遇");
bundle.putParcelable("b", b);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
內容頁面:
package com.example.activity_two;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Two_ac extends Activity {
private TextView tv_two;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two_ac);
imageView=(ImageView) findViewById(R.id.imageView);
tv_two = (TextView) findViewById(R.id.tv_two);
Intent intent = getIntent();
if (intent != null) {
String name = intent.getStringExtra("title");
Bitmap bitmap=intent.getParcelableExtra("b");
tv_two.setText(name);
imageView.setImageBitmap(bitmap);
}
}
}
效果圖片
————————-跳轉————————-
源碼地址爲:http://download.csdn.net/detail/zhan1350441670/8958925