码迷,mamicode.com
首页 > 移动开发 > 详细

Android--activity获取返回值

时间:2016-03-30 11:03:39      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

写了一个DEMO:短信选择联系人的数据传递

主activity:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	}

	public void click(View c){
		//跳转至选择联系人Activity
		Intent intent = new Intent(this, ContactActivity.class);
//		startActivity(intent);
		//用这个api启动的Activity,在销毁时,系统会回调onActivityResult
		startActivityForResult(intent, 10);
	}
	
	
	public void click2(View v){
		//跳转至选择快捷回复的Activity
		Intent intent = new Intent(this, CallbackActivity.class);
		startActivityForResult(intent, 20);
	}
	//如果有Activity在销毁时返回了数据,那么就会调用此方法来接收数据
	//requestCode:用来区分数据来自于哪一个Activity
	//resultCode:用来区分返回的数据是什么类型的
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		String name = data.getStringExtra("name");
		if(requestCode == 10){
			EditText et = (EditText)findViewById(R.id.et);
			et.setText(name);
		}
		else if(requestCode == 20){
			EditText et_content = (EditText)findViewById(R.id.et_content);
			et_content.setText(name);
		}
	}
	

}
联系人activity:

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;

public class ContactActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		
		final String[] objects = new String[]{
				"小志",
				"逼哥",
				"世界级XXX",
				"国服第一"
		};
		
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));
	
		//给listview设置条目的点击侦听
		lv.setOnItemClickListener(new OnItemClickListener() {

			//当某个条目被点击时,此方法调用
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				//Activity返回时传递数据,也是通过意图对象
				Intent data = new Intent();
				//把要传递的数据封装至意图对象中
				data.putExtra("name", objects[position]);
				
				//当前Activity销毁时,data这个意图就会传递给启动当前Activity的那个Activity
				setResult(1, data);
				
				//销毁当前Activity
				finish();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
}

快捷回复activity:

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;

public class CallbackActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		
		final String[] objects = new String[]{
				"免谈,没戏,滚犊子",
				"媳妇我错了,求原谅",
				"老子才是一家之主"
		};
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent data = new Intent();
				data.putExtra("name", objects[position]);
				
				setResult(2, data);
				finish();
				
			}
		});
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <EditText
        android:id="@+id/et"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="请输入联系人"
        />
    <Button 
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:onClick="click"
        />
    </LinearLayout>
    <EditText
        android:id="@+id/et_content"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:hint="请输入短信内容"
        android:gravity="top"
        />
	<Button 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="快捷回复"
	    android:onClick="click2"
	    />
	<Button 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="发送短信"
	    />
</LinearLayout>

<?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/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>
</LinearLayout>

<?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" >
    

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        />
</LinearLayout>



Android--activity获取返回值

标签:

原文地址:http://blog.csdn.net/chaoyu168/article/details/51011711

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!