android有返回结果的 Activity
2016-12-09 19:52
253 查看
package com.example.android.active; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; /** * Activity实现返回结果 * 1.需要得到activity的返回结果,必须使用startActivityForResult()方法启动另一个activity * 2.必须重写onActivityResult()方法来处理返回结果 * 3.在返回结果的activity中要使用setResult()方法设置结果 * * */ public class MainActivity3 extends Activity implements OnClickListener{ private Button button1; private EditText etNumber; private static final int REQUESTCODE=1; //请求编码,只是做一个标记,以便在onActivityResult()中识别 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); button1=(Button) findViewById(R.id.submit1); button1.setOnClickListener(this); etNumber=(EditText) findViewById(R.id.EditPhoneNumber); } @Override public void onClick(View v) { // TODO Auto-generated method stub //启动一个有返回结果的Activity Intent intent=new Intent(this,MainActivity4.class); //参数:1.intent对象 2.请求编码(标记)可以是正整数值 startActivityForResult(intent, REQUESTCODE); } //重写返回结果的方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUESTCODE: if(resultCode==RESULT_OK){ String phone=data.getStringExtra("phone"); etNumber.setText(phone); } break; default: break; } } }
package com.example.android.active; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity4 extends Activity implements OnItemClickListener{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); listView=(ListView) findViewById(R.id.listView1); String [] array={"123","334","435"}; ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,array); listView.setAdapter(adapter); listView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { // TODO Auto-generated method stub TextView textView=(TextView)v; String s=(String) textView.getText().toString(); System.out.println(s); //设置返回的结果 Intent intent=new Intent(); intent.putExtra("phone", s); this.setResult(RESULT_OK, intent); this.finish(); } }activity_main3.xml
<?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/EditPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入" /> <Button android:id="@+id/submit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>activity_main4
<?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" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>谷歌中国
相关文章推荐
- Android中启动其他Activity并返回结果
- 【Android】启动其他Activity并返回结果
- ANDROID中ACTIVITY处理返回结果的实现方式
- Android进阶03——调用另一个Activity并返回结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android中Activity处理返回结果的实现方式
- (Android review)打开Activity返回结果
- Android开启新的activity并获取返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android Activity启动、关闭、Activity返回结果到启动它的Activity
- Android 启动另外activity并返回结果
- Android中启动其他Activity并返回结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android--启动另一个Activity并返回结果
- 3月23日,从子 activity 获取返回结果,Android 开发,每日20行,第14天。
- 解决Android中Fragment调用startactivityforresult不能返回结果的问题
- Android中activity处理返回结果的实现方式
- android activity之间传递参数,获取上一个activity的返回结果
- android: startActivityForResult用法(启动其他Activity并返回结果)
- Android Activity返回结果startActivityForResult函数