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()就麻烦一点,下面这张图方便我们的理解:
说明一下:
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的生命周期(图文详解)
转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai 联系方式:JohnTsai.Work@gmail.com [Andro ...
- 【Android开发日记】之入门篇(四)——Android四大组件之Activity
在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...
- Android四大组件之Activity(活动)及其布局的创建与加载布局
Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ...
- Android四大组件及activity的四大启动模式
Android四大组件 1. 广播接收者的两种类型: (1)系统广播接收者,就是继承BroadcastReceiver这个类,然后还要在清单文件中注册,注册之后给他一个action.当系统发生了这个a ...
- Android四大组件之——Activity(一)定义、状态和后退栈(图文详解)
什么是Activity 关键字:应用组件.四大组件.用户界面,交互. An Activity is an application component that provides a screen wi ...
- Android 四大组件之 Activity(二)
1.综述 Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满 ...
- Android 四大组件之 Activity
1 简介 Activity (活动) 即应用程序 显示的 界面.可以通过两种方式 设置显示的内容 1:纯代码方式 2:xml 布局方式 无论哪一种方式,都是通过 setContentView 来设置显 ...
- Android 四大组件之 Activity(一)
1.Activity的定义及作用: Android系统中的四大组件之一,可以用于显示View.Activity是一个与用户交互的系统模块,几乎所有的Activity都是和用户进行交互的一个应用程序的组 ...
- [Android基础]Android四大组件之Activity总结
1.Activity简介 Activity是Android一个非常重要的用户接口(四大组件之一),是可见的,主要是用户和应用程序之间进行交互的接口.在每个Activity中都可以放很多控件,所以也可以 ...
随机推荐
- OAuth2.0 授权的工作原理
作者:Barret李靖链接:https://www.zhihu.com/question/19781476/answer/81020455来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...
- JDBC插入数据超长时无法自动截断问题
问题 JDBC操作MySQL数据库,当进行插入或更新操作的数据长度超过表字段的声明最大长度时,会报出以下错误,导致不能正常插入: 但是当直接在MySQL客户端操作时,发现确实可以的,只不过会自动对插入 ...
- Python使用4个空格替换Tab, TabError: inconsistent use of tabs and spaces in indentation。
问题:以前使用Pycharm和VsCode没遇到问题,使用nodepat++老是提示Tab异常 TabError: inconsistent use of tabs and spaces in in ...
- Java编程的逻辑 (41) - 剖析HashSet
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Drupal Nginx伪静态设置方法
location ~ ^.*/files\/styles\/.*$ { access_log off; expires 45d; error_page @drupal; } location @dru ...
- 【C】——C语言字符串比较函数
c语言字符串函数详解 void *memset(void *dest, int c, size_t count); 将dest前面count个字符置为字符c. 返回dest的值. void *memm ...
- 反射setAccessible()方法
java代码中,常常将一个类的成员变量置为private 在类的外面获取此类的私有成员变量的value时,需要注意: 测试类: public class AccessibleTest { privat ...
- linux安装android sdk
https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip 1,先安装java https://developer.and ...
- QTcpSocket 发送和接收数据的几种方法
1.QTcpSocket 继承于QAbstractSocket继承于QIODevice 2.QTcpSocket 提供的几种接收和发送数据方法 write ( const char *, qint64 ...
- 使用VS2012生成DLL文件(1)
文章转载自:1. http://blog.sina.com.cn/s/blog_45bcb4c3010140b3.html 一:生成DLL 1:创建DLL工程 文件->新建->项目-> ...