[Android四大組件學習系列Activity篇]
1.Android四大組件之——Activity(一)定義、狀態和後退棧(圖文詳解)
2.Android四大組件之——Activity的生命周期(圖文詳解)
3.Android四大組件之——Activity的開啟StartActivity()和StartActivityForResult()(圖文詳解)
前兩次學習了Activity的基本概念和生命周期,今天學習一下Activity的跳轉和值傳遞。
廢話不多說,開始學習!
Activity的開啟
開啟一個新的Activity有兩種方法:
startActivity(Intent)和startActivityForResult(Intent,int)。
startActivity(Intent):
用於開啟一個新的Activity,新Activity壓入棧頂。參數Intent用於描述新Activity。
startActivityForResult(Intent,int):
用於開啟新Activity,新Activity退出後,返回結果給舊Activity。
startActivity好理解,startActivityForResult()就麻煩一點,下面這張圖方便我們的理解:
說明一下:
requestCode,請求碼,自己隨意設置,但必須是非負數。只有是非負數時,Activity退出時,這個碼會返回到onActivityResult()。
resultCode,結果碼,用於標識返回數據來自於哪個新Activity,它的值一般是
RESULT_CANCELED, RESULT_OK, RESULT_FIRST_USER。
寫了一個關於開啟Activity的Demo:
共三個Activity:MainActivity用戶填寫用戶姓名電話城市、CityActivity用於用戶選擇城市,ShowActivity用於展示用戶信息。
MainActivity:
復制代碼
1 package com.johntsai.startactivity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.database.Cursor;
6 import android.net.Uri;
7 import android.os.Bundle;
8 import android.provider.ContactsContract.CommonDataKinds.Phone;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.EditText;
13
14 public class MainActivity extends Activity implements OnClickListener {
15
16 private Button button_city,button_phone,button_save;
17 private EditText editText_city,editText_name,editText_phone;
18 private static final int SELECT_CITY_REQUEST =2;
19 private static final int SELECT_PHONE_REQUEST =3;
20
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.activity_main);
25
26 button_city = (Button)findViewById(R.id.button_city);
27 button_phone = (Button)findViewById(R.id.button_phone);
28 button_save = (Button)findViewById(R.id.button_save);
29 editText_city = (EditText)findViewById(R.id.editText_city);
30 editText_name = (EditText)findViewById(R.id.editText_name);
31 editText_phone = (EditText)findViewById(R.id.editText_Phone);
32
33 button_city.setOnClickListener(this);
34 button_phone.setOnClickListener(this);
35 button_save.setOnClickListener(this);
36 }
37 @Override
38 public void onClick(View v) {
39 switch (v.getId()) {
40 case R.id.button_city:
41 Intent i = new Intent ();
42 i.setClass(MainActivity.this, CityActivity.class);
43 startActivityForResult(i, SELECT_CITY_REQUEST);
44 break;
45 case R.id.button_phone:
46 Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
47 pickContactIntent.setType(Phone.CONTENT_TYPE);
48 startActivityForResult(pickContactIntent, SELECT_PHONE_REQUEST);
49 break;
50 case R.id.button_save:
51 String name=editText_name.getText().toString();
52 String city = editText_city.getText().toString();
53 String phone = editText_phone.getText().toString();
54 Intent saveIntent = new Intent(MainActivity.this,ShowActivity.class);
55 saveIntent.putExtra("content", "NAME: "+name+"\nCITY: "+city+"\nPHONE"+phone);
56 startActivity(saveIntent);
57 break;
58 default:
59 break;
60 }
61 }
62
63 @Override
64 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
65 if(SELECT_CITY_REQUEST == requestCode){
66 if(resultCode ==RESULT_OK){
67 String city = data.getStringExtra("city");
68 editText_city.setText(city);
69 }
70 }
71 if(SELECT_PHONE_REQUEST == requestCode){
72 if(resultCode == RESULT_OK){
73 Uri contactData = data.getData();
74 String [] projection ={Phone.NUMBER};
75 Cursor c = getContentResolver()
76 .query(contactData, projection, null, null, null);
77 c.moveToFirst();
78 int column =c.getColumnIndex(Phone.NUMBER);
79 String num = c.getString(column);
80 editText_phone.setText(num);
81 }
82 }
83 super.onActivityResult(requestCode, resultCode, data);
84 }
85 }
1 package com.johntsai.startactivity;
2
3
4 import android.app.Activity;
5 import android.content.Intent;
6 import android.os.Bundle;
7 import android.view.Gravity;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.AbsListView;
11 import android.widget.BaseExpandableListAdapter;
12 import android.widget.ExpandableListAdapter;
13 import android.widget.ExpandableListView;
14 import android.widget.LinearLayout;
15 import android.widget.TextView;
16 import android.widget.Toast;
17
18 public class CityActivity extends Activity {
19
20 private ExpandableListView eListView;
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.layout_city);
26 initEListView();
27 }
28
29 //初始化
30 private void initEListView(){
31 ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
32
33
34 private String [] countries = new String[]{
35 "China","USA","UK"
36 };
37
38 private String [][] cities = new String[][]{
39 {"BeiJing","ShangHai","HongKong","TaiBei"},
40 {"Washington","New York","Los Angles","San Fransico"},
41 {"London","Manchester","Birmingham","Liverpool"}
42 };
43 private TextView getTextView(){
44 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
45 ViewGroup.LayoutParams.WRAP_CONTENT);
46 TextView textView = new TextView(CityActivity.this);
47 textView.setLayoutParams(lp);
48 textView.setGravity(Gravity.CENTER_HORIZONTAL);
49 textView.setPadding(0, 0, 0, 0);
50 textView.setTextSize(20);
51 return textView;
52
53 }
54 @Override
55 public boolean isChildSelectable(int groupPosition, int childPosition) {
56 Toast.makeText(CityActivity.this, "you have clicked Group:"+groupPosition+"Child:"+childPosition+"\n"+countries[groupPosition]+" "+cities[groupPosition][childPosition],
57 Toast.LENGTH_SHORT).show();
58 Intent i = new Intent();
59 i.putExtra("city", cities[groupPosition][childPosition]);
60 setResult(RESULT_OK, i);
61 finish();
62 return true;
63 }
64
65 @Override
66 public boolean hasStableIds() {
67 return true;
68 }
69
70 @Override
71 public View getGroupView(int groupPosition, boolean isExpanded,
72 View convertView, ViewGroup parent) {
73 LinearLayout linearLayout = new LinearLayout(CityActivity.this);
74 linearLayout.setOrientation(0);
75
76 TextView textView = getTextView();
77 textView.setText(getGroup(groupPosition).toString());
78 linearLayout.addView(textView);
79 return linearLayout;
80 }
81 @Override
82 public long getGroupId(int groupPosition) {
83 return groupPosition;
84 }
85
86 @Override
87 public int getGroupCount() {
88 return countries.length;
89 }
90
91 @Override
92 public Object getGroup(int groupPosition) {
93 return countries[groupPosition];
94 }
95
96 @Override
97 public int getChildrenCount(int groupPosition) {
98 return cities[groupPosition].length;
99 }
100
101 @Override
102 public View getChildView(int groupPosition, int childPosition,
103 boolean isLastChild, View convertView, ViewGroup parent) {
104 TextView textView = getTextView();
105 textView.setText(getChild(groupPosition, childPosition).toString());
106 textView.setGravity(Gravity.CENTER_HORIZONTAL);
107 return textView;
108 }
109
110 @Override
111 public long getChildId(int groupPosition, int childPosition) {
112 return childPosition;
113 }
114
115 @Override
116 public Object getChild(int groupPosition, int childPosition) {
117 return cities[groupPosition][childPosition];
118 }
119 };
120 eListView = (ExpandableListView)findViewById(R.id.expandableListView);
121 eListView.setAdapter(adapter);
122 }
123 }
復制代碼
復制代碼
1 package com.johntsai.startactivity;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.widget.TextView;
7
8 public class ShowActivity extends Activity {
9 private TextView showTextView;
10 @Override
11 protected void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.layout_show);
14 showTextView = (TextView)findViewById(R.id.textView_show);
15
16 Intent i =getIntent();
17 String content = i.getStringExtra("content");
18 showTextView.setText(content);
19 }
20 }