´«µÝÊý¾Ý
- н¨ Activity
ÔÚÏîÄ¿ÖÐÓÒ¼ü£¬Ñ¡Ôñ New£¬¼ÌÐøÑ¡ÔñÖÐÏ·½µÄ Activity£¬×îºóÊÇ Blank Activity¡£
´Ëʱ Android Studio »áΪÎÒÃÇ×Ô¶¯Ìí¼ÓÒ»¸ö¶ÔÓ¦µÄ²¼¾ÖÎļþ£¬Îª²¼¾ÖÎļþÖÐµÄ TextView Ìí¼Ó ID£¬ÒÔ±ãºóÐøʹÓá£
- ÓÃIntent ·¢ËÍÊý¾Ý
ÔÚÖ÷²¼¾ÖÎļþÖÐн¨Ò»¸ö Button ¿Ø¼þ£¬²¢ÔÚÖ÷ÀàÖж¨ÒåËü£¬È»ºóΪËüÉèÖà Click µÄ¼àÌýʼþ¡£
Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); i.putExtra("Thanks","Thank you for reading my blog."); startActivity(i); } }); }
- Óà Intent ½ÓÊÕÊý¾Ý
½ÓÏÂÀ´ÔÙ֮ǰ´´½¨µÄ Activity ÖÐͬÑùн¨Ò»¸ö Intent ½øÐнÓÊܼ´¿É¡£ºÍ·¢ËÍÊÔÓÃµÄ putExtra ÀàËÆ£¬½ÓÊÜÒ²¿ÉÒÔÓà getStringExtra ¡£µ±È»ÁË£¬Õý¶Ô²»Í¬µÄÊý¾ÝÀàÐÍ¿ÉÒÔÓò»Í¬µÄ·½·¨¡£
private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); tv = (TextView)findViewById(R.id.tv); tv.setText(i.getStringExtra("Thanks")); }
´«µÝ Bundle °ü
ÔÚÁ½¸ö Activity Ö®¼äÒ²¿ÉÒÔÓà Bundle °üÀ´½øÐÐÊý¾ÝµÄ´«µÝ£¬Ïà±ÈÉÏÒ»ÖÖ·½Ê½£¬ÕâÒ»ÖÖ¿ÉÒÔ°üº¬¸ü¶àµÄÊý¾Ý£¬Äã¿ÉÒÔ½«ÆäÈÏΪÊÇÒ»¸ö¼¯ºÏ£¬ÕâÒ²Õý·ûºÏ¡°°ü¡±µÄ¸ÅÄî¡£
н¨ Bundle
Bundle ¿ÉÒÔǶÌ× Bundle
¶Ô Intent ¶ÔÏóʹÓà putExtras ·½·¨
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); Bundle a = new Bundle(); a.putString("Thanks", "Thank you for reading my blog."); Bundle b = new Bundle(); b.putString("Android","Google"); b.putString("Windows","Microsoft"); a.putBundle("bundle",b); i.putExtras(a); startActivity(i); } });}
- ¸ù¾ÝÏàÓ¦µÄ Key Öµ»ñÈ¡ÏàÓ¦µÄBundleÊý¾Ý
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); Bundle a2 = i.getExtras(); tv = (TextView)findViewById(R.id.tv); tv.setText(String.format("%s AndroidÊôÓÚ%s£¬WindowsÊôÓÚ%s", a2.getString("Thanks"), a2.getBundle("bundle").getString("Android"), a2.getBundle("bundle").getString("Windows"))); }
´«µÝ×Ô¶¨Òå¶ÔÏó
Android ¿ª·¢¹ý³ÌÖÐ×ÔÈ»ÉÙ²»ÁË¡°ÀࡱÁË£¬ÄÇôÕâÖÖÊý¾ÝÓÖÈçºÎ´«ÊäÄØ¡£
- ÇмǣºÒ»¶¨ÒªÖ´ÐÐÐòÁл¯£¬¼´¼ÓÉÏ¡°implements Serializable¡±
public class Source implements Serializable{ private int source; private String ID; public int getSource() { return source; } public void setSource(int source) { this.source = source; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public Source(String ID, int source) { this.source = source; this.ID = ID; }}
ÏàÓ¦ÓÐÁËÇ°ÃæµÄÁ˽⣬´ó¼Ò¶¼¿ÉÒÔÖ±½Ó²Âµ½ÈçºÎʹÓÃÁË£¬ÄÇÎҾͲ»ÔÙ†ªàÂÁË¡£
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); i.putExtra("source",new Source("Player",1000)); startActivity(i); } });}
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); Source s = (Source) i.getSerializableExtra("source"); tv = (TextView) findViewById(R.id.tv); tv.setText(String.format("IDÊÇ%s£¬µÃ·ÖÊÇ%d", s.getID(), s.getSource())); }
´ÓActivity·µ»ØÊý¾Ý
Óû§ÐèÒªÓëÓ¦ÓýøÐн»»¥£¬Ó¦ÓÃÐèÒª½«Óû§ÊäÈëµÄÊý¾Ý½øÐзµ»Ø£¬ËùÒÔ¡¡
ÔÚÖ÷ Activity Öж¨Òå Button ºÍ TextView£¬Ç°ÕßÓÃÓÚµ¼º½ÖÁ´Î Activity £¬ ºóÕßÓÃÓÚÏÔʾ·µ»ØµÄÊý¾Ý¡£
ÔÚ´Î Activity Öж¨Òå Button ºÍ EditText£¬Ç°ÕßÓÃÓÚÈ·Èϲ¢·µ»ØÖÁÖ÷ Activity£¬ºóÕßÓÃÓÚÓû§ÊäÈë¡£
ÔÚ´Î Activity ÖУº
ͬÑùÓà Intent ´«µÝÊý¾Ý
Óà setResult ÉèÖ÷µ»Ø½á¹û
×îºóÓà finish
private Button button ; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); editText = (EditText)findViewById(R.id.editText); button = (Button)findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.putExtra("info",editText.getText().toString()); setResult(0, i); // 0±íʾ³É¹¦ finish(); } }); }
ÔÚÖ÷ Activity ÖУº
- Óà startActivityForResult ½øÐе¼º½£¬²¢ÇëÇó½á¹û
private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); startActivityForResult(i,0); } }); }
- ÖØÔØ onActivityResult ·½·¨£¬²¢Í¨¹ý data ²ÎÊý»ñÈ¡Êý¾Ý¡£
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); textView.setText(data.getStringExtra("info")); }
ºÃÁË£¬Activity Ö®¼ä¾ÍÊÇÕâô´«µÝÊý¾ÝµÄ¡¡
Áãµã°ëÁË¡¡Ë¯¾õ¡¡
±¾ÎÄΪÖظ´ÎÄÕ£¬½öΪ²âÊÔ¡£
°æȨÉùÃ÷£º±¾ÎÄΪ NoMasp¿ÂÓÚÍú Ô´´ÎÄÕ£¬Î´¾Ðí¿ÉÑϽûתÔØ£¡»¶Ó·ÃÎÊÎҵIJ©¿Í£ºhttp://blog.csdn.net/nomasp