編輯:關於Android編程
之前項目需要,找過這方面知識。所以今天也總結一下。大家都知道activity跳轉用intent,Android的當前頁面跳轉到新的頁面。當然跳轉的同時常常要攜帶數據或者對象。那我下面就說說跳轉帶對象吧。還有在例子當中,新的activity返回時,也有對象一起返回。下面看代碼,只要入門Android了都看得懂,我就不再寫很多注釋了。
有兩種對象類型可以傳遞,一是Parcelable,推薦用這個。二是Serializable。當然你在class建立對象時,都要對這兩個進行接口。看代碼。
package com.example.intentdemo; import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable{ private String name; private String sex; private int eage; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getEage() { return eage; } public void setEage(int eage) { this.eage = eage; } public static final Parcelable.CreatorCREATOR = new Creator () { public User createFromParcel(Parcel source) { User user = new User(); user.name = source.readString(); user.sex = source.readString(); user.eage = source.readInt(); return user; } public User[] newArray(int size) { return new User[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int arg1) { parcel.writeString(name); parcel.writeString(sex); parcel.writeInt(eage); } }
package com.example.intentdemo; import java.io.Serializable; public class Student implements Serializable { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getEage() { return eage; } public void setEage(int eage) { this.eage = eage; } private int eage; }
package com.example.intentdemo; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private ArrayListmArrayListUsers = new ArrayList (); private ArrayList mArrayListStudent = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitUI(); } private void InitUI() { Button Parcelable = (Button) findViewById(R.id.button1); Button Serializable = (Button) findViewById(R.id.button2); if (Parcelable != null) { Parcelable.setOnClickListener(this); } if (Serializable != null) { Serializable.setOnClickListener(this); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { if (arg0.getId() == R.id.button1) { setParcelable(); } else if (arg0.getId() == R.id.button2) { setSerializable(); } } private void setSerializable() { Student student = new Student(); student.setName(Serializable); student.setSex(nv); student.setEage(25); mArrayListStudent.add(student); mArrayListStudent.add(student); Intent mIntent = new Intent(this, IntentSerializableActivity.class); Bundle mBundle = new Bundle(); mBundle.putSerializable(student_key, student); // 傳遞一個user對象 mBundle.putSerializable(student_Arry, mArrayListStudent); // 傳遞一個user對象列表 mIntent.putExtras(mBundle); startActivityForResult(mIntent, 200); // 帶返回結果的Intent,標志碼是1 } private void setParcelable() { User user = new User(); user.setName(Parcelable); user.setSex(nan); user.setEage(25); mArrayListUsers.add(user); mArrayListUsers.add(user); Intent mIntent = new Intent(this, IntentParcelableActivity.class); Bundle mBundle = new Bundle(); mBundle.putParcelable(User_key, user); // 傳遞一個user對象 mBundle.putParcelableArrayList(User_Arry, mArrayListUsers); // 傳遞一個user對象列表 mIntent.putExtras(mBundle); startActivityForResult(mIntent, 100); // 帶返回結果的Intent,標志碼是1 } // 返回activity頁面刷新 protected void onActivityResult(int requestCode, int resultCode, Intent data) { TextView show = (TextView) findViewById(R.id.test); if (resultCode == RESULT_OK && requestCode == 100) { Bundle bundle = this.getIntent().getExtras(); if (bundle != null) { mArrayListUsers = bundle.getParcelableArrayList(User_Arry); } show.setText(); show.append(IntentParcelableActivity返回的數據: + ); for (int i = 0; i < mArrayListUsers.size(); i++) { User userArrayList = new User(); userArrayList = mArrayListUsers.get(i); show.append(name is: + userArrayList.getName() + + age is: + userArrayList.getEage() + ); } } else if (resultCode == RESULT_OK && requestCode == 200) { Bundle bundle = this.getIntent().getExtras(); if (bundle != null) { mArrayListStudent = (ArrayList ) bundle .getSerializable(student_Arry); } show.setText(); show.append(IntentSerializableActivity返回的數據: + ); for (int i = 0; i < mArrayListStudent.size(); i++) { Student studentArrayList = new Student(); studentArrayList = mArrayListStudent.get(i); show.append(name is: + studentArrayList.getName() + + age is: + studentArrayList.getEage() + ); } } } }
IntentParcelableActivity.class
package com.example.intentdemo; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.KeyEvent; import android.view.Menu; import android.widget.TextView; public class IntentParcelableActivity extends Activity { private ArrayListmArrayListUsers = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView mTextView = new TextView(this); User user = new User(); Bundle bundle = this.getIntent().getExtras(); if (bundle != null) { user = bundle.getParcelable(User_key); mArrayListUsers = bundle.getParcelableArrayList(User_Arry); } mTextView.append(You name is: + user.getName() + + You age is: + user.getEage() + + ); for (int i = 0; i < mArrayListUsers.size(); i++) { User userArrayList = new User(); userArrayList = mArrayListUsers.get(i); mTextView.append(You name is: + userArrayList.getName() + + You age is: + userArrayList.getEage() + ); } setContentView(mTextView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } // 返回按鍵調用 public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(); Bundle mBundle = new Bundle(); mBundle.putParcelableArrayList(User_Arry, mArrayListUsers); //傳遞一個user對象列表 intent.putExtras(mBundle); setResult(RESULT_OK, intent); finish(); } return true; } }
package com.example.intentdemo; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.KeyEvent; import android.view.Menu; import android.widget.TextView; public class IntentSerializableActivity extends Activity { private ArrayListmArrayListStudent=new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView mTextView = new TextView(this); Student student = new Student(); Bundle bundle = this.getIntent().getExtras(); if (bundle != null) { student = (Student) bundle.getSerializable(student_key); mArrayListStudent = (ArrayList ) bundle.getSerializable(student_Arry); } mTextView.append(You name is: + student.getName() + + You age is: + student.getEage() + + ); for (int i = 0; i < mArrayListStudent.size(); i++) { Student studentArrayList = new Student(); studentArrayList = mArrayListStudent.get(i); mTextView.append(You name is: + studentArrayList.getName() + + You age is: + studentArrayList.getEage() + ); } setContentView(mTextView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } // 返回按鍵調用 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(); Bundle mBundle = new Bundle(); mBundle.putSerializable(student_Arry, mArrayListStudent); //傳遞一個user對象列表 intent.putExtras(mBundle); setResult(RESULT_OK, intent); finish(); } return true; } }
其實看一下代碼,都是能看明白。也很容易實踐,用過一遍之後就好很多了。
魅族MX6已發布了,除了漂亮的外觀,MX6還帶來了一項重磅功能:微信指紋支付(此前已經支持支付寶)。那魅族微信指紋支付是怎麼支付的呢?下文就讓小編跟大家魅族
前些天做了一個應用,因為怕影響用戶體驗,所以我將廣告設定了一下,就是每啟動軟件8次,就會彈出一次廣告。在上傳到應用寶後,居然得到了這樣的結果:看到了吧!無病毒,無廣告。看
認識ReactReact是一個用於構建用戶界面的JavaScript庫。 React主要用於構建UI,很多人認為React是MVC中的V,即視圖。 React起源於Fac
1.Network Protocols RTSP (RTP, SDP) HTTP progressive streaming HTTP live streaming dr