Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

     

      如需转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai

      联系方式:JohnTsai.Work@gmail.com

      [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()就麻烦一点,下面这张图方便我们的理解:

Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

说明一下:

requestCode,请求码,自己随意设置,但必须是非负数。只有是非负数时,Activity退出时,这个码会返回到onActivityResult()。

resultCode,结果码,用于标识返回数据来自于哪个新Activity,它的值一般是

RESULT_CANCELED, RESULT_OK, RESULT_FIRST_USER。

写了一个关于开启Activity的Demo:

共三个Activity:MainActivity用户填写用户姓名电话城市、CityActivity用于用户选择城市,ShowActivity用于展示用户信息。

MainActivity:

 package com.johntsai.startactivity;

 import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener { private Button button_city,button_phone,button_save;
private EditText editText_city,editText_name,editText_phone;
private static final int SELECT_CITY_REQUEST =2;
private static final int SELECT_PHONE_REQUEST =3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button_city = (Button)findViewById(R.id.button_city);
button_phone = (Button)findViewById(R.id.button_phone);
button_save = (Button)findViewById(R.id.button_save);
editText_city = (EditText)findViewById(R.id.editText_city);
editText_name = (EditText)findViewById(R.id.editText_name);
editText_phone = (EditText)findViewById(R.id.editText_Phone); button_city.setOnClickListener(this);
button_phone.setOnClickListener(this);
button_save.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_city:
Intent i = new Intent ();
i.setClass(MainActivity.this, CityActivity.class);
startActivityForResult(i, SELECT_CITY_REQUEST);
break;
case R.id.button_phone:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, SELECT_PHONE_REQUEST);
break;
case R.id.button_save:
String name=editText_name.getText().toString();
String city = editText_city.getText().toString();
String phone = editText_phone.getText().toString();
Intent saveIntent = new Intent(MainActivity.this,ShowActivity.class);
saveIntent.putExtra("content", "NAME: "+name+"\nCITY: "+city+"\nPHONE"+phone);
startActivity(saveIntent);
break;
default:
break;
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(SELECT_CITY_REQUEST == requestCode){
if(resultCode ==RESULT_OK){
String city = data.getStringExtra("city");
editText_city.setText(city);
}
}
if(SELECT_PHONE_REQUEST == requestCode){
if(resultCode == RESULT_OK){
Uri contactData = data.getData();
String [] projection ={Phone.NUMBER};
Cursor c = getContentResolver()
.query(contactData, projection, null, null, null);
c.moveToFirst();
int column =c.getColumnIndex(Phone.NUMBER);
String num = c.getString(column);
editText_phone.setText(num);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
 package com.johntsai.startactivity;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class CityActivity extends Activity { private ExpandableListView eListView;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.layout_city);
initEListView();
} //初始化
private void initEListView(){
ExpandableListAdapter adapter = new BaseExpandableListAdapter() { private String [] countries = new String[]{
"China","USA","UK"
}; private String [][] cities = new String[][]{
{"BeiJing","ShangHai","HongKong","TaiBei"},
{"Washington","New York","Los Angles","San Fransico"},
{"London","Manchester","Birmingham","Liverpool"}
};
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(CityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setPadding(0, 0, 0, 0);
textView.setTextSize(20);
return textView; }
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
Toast.makeText(CityActivity.this, "you have clicked Group:"+groupPosition+"Child:"+childPosition+"\n"+countries[groupPosition]+" "+cities[groupPosition][childPosition],
Toast.LENGTH_SHORT).show();
Intent i = new Intent();
i.putExtra("city", cities[groupPosition][childPosition]);
setResult(RESULT_OK, i);
finish();
return true;
} @Override
public boolean hasStableIds() {
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(CityActivity.this);
linearLayout.setOrientation(0); TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
linearLayout.addView(textView);
return linearLayout;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public int getGroupCount() {
return countries.length;
} @Override
public Object getGroup(int groupPosition) {
return countries[groupPosition];
} @Override
public int getChildrenCount(int groupPosition) {
return cities[groupPosition].length;
} @Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return cities[groupPosition][childPosition];
}
};
eListView = (ExpandableListView)findViewById(R.id.expandableListView);
eListView.setAdapter(adapter);
}
}

CityActivity

 package com.johntsai.startactivity;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class ShowActivity extends Activity {
private TextView showTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_show);
showTextView = (TextView)findViewById(R.id.textView_show); Intent i =getIntent();
String content = i.getStringExtra("content");
showTextView.setText(content);
}
}

ShowActivity

Demo运行效果图

Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)                      Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)                        Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

Android四大组件之——Activity的开启:StartActivity()和StartActivityForResult()(图文详解)

如果喜欢,请推荐!谢谢~

上一篇:【Android】安卓四大组件之Activity(二)


下一篇:安装logstash+kibana+elasticsearch+redis搭建集中式日志分析平台