Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Intent傳遞對象小結

Android Intent傳遞對象小結

編輯:關於Android編程

效果:

\\

\\

\

想看實例的,感興趣的可以看之前的一篇文章

Android ViewPager Fragment實現選項卡


部分關鍵代碼:

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">public class SerializeActivity extends Activity implements Serializable { Button btnlist, btnParcelable, btnSerialze; private final String TAG = "SerializeActivity"; public final static String LIST_KEY = "ArrayList"; public final static String PAR_KEY = "parcelable"; public final static String SER_KEY = "serializable"; private static final long serialVersionUID = 1L; private ArrayList m_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_serialize); initView(); initlist(); } /* * 初始化視圖 */ public void initView() { btnlist = (Button) findViewById(R.id.btnlist); btnParcelable = (Button) findViewById(R.id.btnParcelable); btnSerialze = (Button) findViewById(R.id.btnSerialze); btnlist.setOnClickListener(btnlistener); btnParcelable.setOnClickListener(btnlistener); btnSerialze.setOnClickListener(btnlistener); } public void initlist() { m_list = new ArrayList(); m_list.add("www.88ios.com"); m_list.add("Android移動開發"); m_list.add("IOS開發入門"); } /* * 按鈕點擊事件 */ OnClickListener btnlistener = new OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.btnlist: intentList(); break; case R.id.btnParcelable: intentParcelabe(); break; case R.id.btnSerialze: intentShowSer(); break; } } }; /* * */ public void intentList() { Intent list_intent = new Intent(); list_intent.putStringArrayListExtra(LIST_KEY, m_list); list_intent.setClass(SerializeActivity.this, ShowInfo.class); startActivity(list_intent); } /* * */ public void intentParcelabe() { Student m_Student = new Student(); m_Student.setName("88ios.com"); m_Student.setAge(25); m_Student.setSex("男"); Intent p_Intent = new Intent(SerializeActivity.this, ShowParcelabe.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(PAR_KEY, m_Student); p_Intent.putExtras(mBundle); startActivity(p_Intent); } /* * */ public void intentShowSer() { final SerializableMap myMap = new SerializableMap(); List> list = new ArrayList>(); Map maplist; Map paramMap4 = new HashMap(); paramMap4.put("name", "洪生鵬"); paramMap4.put("website", "www.88ios.com"); paramMap4.put("address", "廣州"); myMap.setMap(paramMap4); SerializableMap serializableMap = (SerializableMap) myMap; maplist = serializableMap.getMap(); list.add(maplist); Intent mIntent = new Intent(SerializeActivity.this, ShowSer.class); Bundle mBundle = new Bundle(); mBundle.putSerializable("map", (Serializable) myMap); //mBundle.putSerializable("map", (Serializable) list); mIntent.putExtras(mBundle); startActivity(mIntent); } } ShowInfo.java

public class ShowInfo extends Activity {

	private Intent list_intent;
	private ArrayList m_arrayList;
	private TextView textview;

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

		textview = (TextView) findViewById(R.id.textview);
		StringBuffer sb = new StringBuffer();

		list_intent = getIntent();
		m_arrayList = list_intent.getExtras().getStringArrayList(
				SerializeActivity.LIST_KEY);
		m_arrayList.get(0);

		sb.append(m_arrayList.get(0) + " \n" + m_arrayList.get(1) + "\n"
				+ m_arrayList.get(2));

		textview.setText(sb);
	}

}

ShowParcelabe.java

public class ShowParcelabe extends Activity {
	private TextView textview;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_parcelabe);
		
		textview = (TextView)findViewById(R.id.textview);  
        Student p_student = (Student)getIntent().getParcelableExtra(SerializeActivity.PAR_KEY);    
        textview.setText("姓名: " + p_student.getName()+"\n"+    
                          "年齡: " + p_student.getAge() + "\n" +    
                          "性別 : " + p_student.getSex() + "\n" +  
                          "類:" + p_student.getClass());    
	}



}

ShowSer.java

public class ShowSer extends Activity {
	private TextView textview;
	StringBuilder sb = new StringBuilder();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_ser);
		textview = (TextView)findViewById(R.id.textview);  
		Map maplist;
		Bundle bundle = getIntent().getExtras();
		SerializableMap serializableMap = (SerializableMap) bundle.get("map");
		maplist = serializableMap.getMap();
		/*
		for (String k : maplist.keySet()) {
			Toast.makeText(this, "" + maplist.get(k), Toast.LENGTH_SHORT)
					.show();
		}
        */
		Set set = maplist.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			String valu = (String) entry.getValue();
			sb.append(key+"  "+valu+"\n");
		}
		textview.setText(sb);   
	}

}



轉載請保留鏈接

http://hongshengpeng.com/article/show/271.aspx


源代碼下載


QQ

交流群:154950206 進群驗證:88ios


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