Android之Starting an Activity for a Result例子简介
来源:互联网 发布:软件配置管理实施情况 编辑:程序博客网 时间:2024/05/21 10:52
有时候我们由一个Activity启动另一个Activity的时候,我们需要从目标Activity中得到处理的信息返回源Activity中,这里就需要用到有返回结果的Activity.
我们源Activity中启动目的Activity要用到startActivityForResult (Intent intent, int requestCode)方法。
其中intent可以包含目标Activity对象或者一些需要处理的信息;requestCode就是对应一个目标Activity的编号,因为一个源Acticity可以启动多个目标Activity.
请看下面代码:
源Acticity:
public class ActivityresultActivity extends Activity{/**发送按钮*/private Button startButton;/**显示返回结果按钮*/private TextView textView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);startButton = (Button) findViewById(R.id.start_result);textView = (TextView) findViewById(R.id.show_result);startButton.setOnClickListener(new OnClickListener(){Intent intent = new Intent();public void onClick(View v){intent.setClass(ActivityresultActivity.this, Response.class);/*将intent装载目的地Activity,经过处理后,将在onActivityResult回调函数中得到想要的信息*/ActivityresultActivity.this.startActivityForResult(intent, 0);}});}/** * 得到返回的信息,并处理 */@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){if(requestCode == 0){if(resultCode == RESULT_OK){/*得到另一个Activity处理后返回的值*/String getValue = data.getStringExtra("getValue");System.out.println(getValue);textView.setText(getValue);}}}}
intent.setClass(ActivityresultActivity.this, Response.class);
其中Response是目标Acitity类.
要得到目标Activity:Response处理后的信息,就要重写回调函数onActivityResult()方法,如上,requestCode就是刚才对应的目标Acitivity的编号,这个用来判断是哪个目标Activity返回来的信息。resultCode是返回的状态,在目标Activity中定义,稍后贴上代码。
这个Activity对应的layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/show_result" android:layout_width="fill_parent" android:layout_height="50px" android:background="@android:color/white" /> <Button android:id="@+id/start_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/start_button" /></LinearLayout>
目标Activity:Response
public class Response extends Activity{private EditText editText;private Button enterButton;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.manipulate);System.out.println("ResultActivity onCreate");editText = (EditText) findViewById(R.id.input_text);enterButton = (Button) findViewById(R.id.enter_finish);enterButton.setOnClickListener(new OnClickListener(){Intent intent = new Intent();public void onClick(View v){intent.setClass(Response.this, ActivityresultActivity.class);String getValue = editText.getText().toString();intent.putExtra("getValue", getValue);Response.this.setResult(RESULT_OK, intent);//当调用finsh()方法时,这个intent就会传递回调用这个Activity的源Activity的onActivityResult()方法里Response.this.finish();}});}}返回状态就在目标Activity的setResult()方法中设置. 以上代码中有注释关键的finish()作用.
Response对应的layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/input_text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/enter_finish" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/enter_button" /></LinearLayout>
程序运行结果图:
源Activity:白色区域为一个TextView,用来显示从目标Activity中得到的信息
目标Activity:白色区域为EditText,输入的值传递回源Activity中
源Activity:得到返回值,并显示
- Android之Starting an Activity for a Result例子简介
- Android之Starting an Activity for a Result例子简介
- Android Activity生命周期 之starting an activity
- android-Building a Notification,Preserving Navigation when Starting an Activity
- Android training 管理Activity生命周期之启动Activity(Starting an Activity)
- Getting a Result from an Activity
- 18Getting a Result from an Activity
- Getting a Result from an Activity
- Getting a Result from an Activity
- Starting an Activity
- 05Starting an Activity
- Starting an Activity
- Starting an Activity
- [Android Training视频系列]2.1 Starting an Activity
- Android官方文档翻译 十七 4.1Starting an Activity
- android之activity 跨Activity传result
- Process of Starting an Activity
- 启动一个Activity - Starting an Activity
- java regex 正则部分高级特性使用
- 农夫养牛
- 慎用mysql的utf8-unicode
- 用实例讲解Variant类型在VB、C#、VC中的参数传递
- 改善网站性能和改善数据库性能
- Android之Starting an Activity for a Result例子简介
- 如何让两个android程序共享资源
- Oracle 分析及动态采样
- 没什么亮点的第一篇呐…C++代码,求斐波那契(Fibonacci)数列的第n项,n可以为负数。递归算法,使用了记忆化搜索。
- 开发属于自己的在线编辑器--编辑器的界面
- jndi中在web.xml中配置的说明
- 多线程---(互斥体)
- 开发属于自己的在线编辑器--编辑器的核心
- 浙江大学2011年复试上机题 解题报告