从新的activity取得返回值

<!-- 读取联系人信息权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>

package com.example.send;

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;

public class MainActivity extends Activity implements OnClickListener {
	private EditText et_contact;
	private EditText et_contact2;
	private Button bu_contact_number;
	private Button bu_contact_number2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_contact = (EditText) findViewById(R.id.et_contact);
		et_contact2 = (EditText) findViewById(R.id.et_contact2);
		bu_contact_number = (Button) findViewById(R.id.bu_contact_number);
		bu_contact_number.setOnClickListener(this);
		bu_contact_number = (Button) findViewById(R.id.bu_contact_number2);
		bu_contact_number.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bu_contact_number:
			//设置启动的activity,请求码为1
			Intent intent = new Intent(this, SelectContactActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.bu_contact_number2:
			//设置启动的activity,请求码为2
			Intent intent2 = new Intent(this, SelectContactActivity.class);
			startActivityForResult(intent2, 2);
			break;
		}
	}

	//这个方法为当前activity开启另一个新的activity之后,在新的activity关闭的时候调用的方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//取得返回的数据
		if (data != null) {
			String number = data.getStringExtra("number");
		//如果点击事件请求码为1,把数据设置到第一个编辑框内
			if (requestCode == 1) {
				System.out.println("bug-----8");
				et_contact.setText(number);
				//如果点击事件请求码为2,把数据设置到第二个编辑框内
			} else if (requestCode == 2) {
				System.out.println("bug-----7");
				et_contact2.setText(number);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}


package com.example.send;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.send.domain.ContactInfo;

public class SelectContactActivity extends Activity {
	private ListView lv_select_contact;
	private List<ContactInfo> contactInfos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lv_select_contact);
		lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);
		// 内容提供者查询出来返回联系人的集合,也就是所有联系人的数据
		contactInfos = ContactService.getContactInfos(this);
		lv_select_contact.setAdapter(new ContactAdapter());
	
		//设置在listview里面点击一个条目的点击事件
		lv_select_contact.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ContactInfo info = contactInfos.get(position);
				String number = info.getNumber();
				Intent data = new Intent();
				data.putExtra("number", number);
				setResult(0, data);
				//结束当前界面,把数据带回给原来的界面
				finish();
			}
		});
	}

	private class ContactAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			//返回集合的大小
			return contactInfos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//设置集合的位置
			ContactInfo info = contactInfos.get(position);
			View view = View.inflate(SelectContactActivity.this, R.layout.item_contact, null);
			TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
			tv_name.setText(info.getName());
			TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
			tv_number.setText(info.getNumber());
			return view;
		}

	}
}


package com.example.send;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.example.send.domain.ContactInfo;

public class ContactService {
	public static List<ContactInfo> getContactInfos(Context context) {
		
		List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
		ContentResolver resolver = context.getContentResolver();
		//系统内容提供者路径content://com.android.contacts/raw_contacts为存放联系人id路径
		//系统内容提供者路径content://com.android.contacts/data为存放联系人数据的路径
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Uri dataUri = Uri.parse("content://com.android.contacts/data");
		Cursor cursor = resolver.query(uri, null, null, null, null);
		while (cursor.moveToNext()){
			//查询的这个表存放联系人id的
			String id = cursor.getString(cursor.getColumnIndex("contact_id"));
			if (id != null){
				//查询的这个表用来存入联系人数据的,以id为标志,一个id,代表一个联系人,一个联系人存放两个数据"data1","mimetype"
				Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{id}, null);
				//查询的一行的数据有两列,一列为data用来存放数据的(0),另一列为mimetype,用来存放数据为哪一类型的(1)				
				ContactInfo conactInfo = new ContactInfo();
				while (dataCursor.moveToNext()){
					//查询表的数据的一列
					String data = dataCursor.getString(0);
					//查询表的数据类型的一列
					String mimetype = dataCursor.getString(1);
					//如果查询的这一行数据类型为名字,就是把这行的数据加入到一人联系人对象的name中,若为phone)v2,就把电话号码加入到联系人对象的number中
					if ("vnd.android.cursor.item/name".equals(mimetype)){
						conactInfo.setName(data);
					} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){
						conactInfo.setNumber(data);
					}
				}
			//把每次查询出来的一个联系人加入到集合中
				contactInfos.add(conactInfo);
				dataCursor.close();
			}
		}
		cursor.close();
		return contactInfos;
	}
}


package com.example.send.domain;

public class ContactInfo {
	private String name;
	private String number;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

}


<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:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/et_number"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:hint="请输入联系人电话号码" />

        <Button
            android:id="@+id/selectContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="联系人" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/et_number2"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:hint="请输入联系人电话号码" />

        <Button
            android:id="@+id/selectContact2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="第二个联系人" />
    </RelativeLayout>

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:hint="请输短信内容"
        android:inputType="textMultiLine"
        android:minLines="5" />

    <Button
        android:id="@+id/sendsms"
        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_selectcontcat"
        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_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="姓名"
        android:textColor="#000000"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_contact_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="电话"
        android:textColor="#990000"
        android:textSize="16sp" />

</LinearLayout>



全部代码:

package com.example.send;

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;

public class MainActivity extends Activity implements OnClickListener {
	private EditText et_contact;
	private EditText et_contact2;
	private Button bu_contact_number;
	private Button bu_contact_number2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_contact = (EditText) findViewById(R.id.et_contact);
		et_contact2 = (EditText) findViewById(R.id.et_contact2);
		bu_contact_number = (Button) findViewById(R.id.bu_contact_number);
		bu_contact_number.setOnClickListener(this);
		bu_contact_number = (Button) findViewById(R.id.bu_contact_number2);
		bu_contact_number.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bu_contact_number:
			Intent intent = new Intent(this, SelectContactActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.bu_contact_number2:
			Intent intent2 = new Intent(this, SelectContactActivity.class);
			startActivityForResult(intent2, 2);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			String number = data.getStringExtra("number");
			if (requestCode == 1) {
				System.out.println("bug-----8");
				et_contact.setText(number);
			} else if (requestCode == 2) {
				System.out.println("bug-----7");
				et_contact2.setText(number);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}


package com.example.send;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.send.domain.ContactInfo;

public class SelectContactActivity extends Activity {
	private ListView lv_select_contact;
	private List<ContactInfo> contactInfos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lv_select_contact);
		lv_select_contact = (ListView) findViewById(R.id.lv_select_contact);
		contactInfos = ContactService.getContactInfos(this);
		lv_select_contact.setAdapter(new ContactAdapter());
	
		lv_select_contact.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ContactInfo info = contactInfos.get(position);
				String number = info.getNumber();
				Intent data = new Intent();
				data.putExtra("number", number);
				setResult(0, data);
				finish();
			}
		});
	}

	private class ContactAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return contactInfos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ContactInfo info = contactInfos.get(position);
			View view = View.inflate(SelectContactActivity.this, R.layout.item_contact, null);
			TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
			tv_name.setText(info.getName());
			TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
			tv_number.setText(info.getNumber());
			return view;
		}

	}
}


package com.example.send;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.example.send.domain.ContactInfo;

public class ContactService {
	public static List<ContactInfo> getContactInfos(Context context) {
		
		List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
		ContentResolver resolver = context.getContentResolver();
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Uri dataUri = Uri.parse("content://com.android.contacts/data");
		Cursor cursor = resolver.query(uri, null, null, null, null);
		while (cursor.moveToNext()){
			String id = cursor.getString(cursor.getColumnIndex("contact_id"));
			if (id != null){
				Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{id}, null);
				ContactInfo conactInfo = new ContactInfo();
				while (dataCursor.moveToNext()){
					String data = dataCursor.getString(0);
					String mimetype = dataCursor.getString(1);
					if ("vnd.android.cursor.item/name".equals(mimetype)){
						conactInfo.setName(data);
					} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)){
						conactInfo.setNumber(data);
					}
				}
				contactInfos.add(conactInfo);
				dataCursor.close();
			}
		}
		cursor.close();
		return contactInfos;
	}
}


package com.example.send.domain;

public class ContactInfo {
	private String name;
	private String number;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

}


<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:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/et_number"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:hint="请输入联系人电话号码" />

        <Button
            android:id="@+id/selectContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="联系人" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/et_number2"
            android:layout_width="match_parent"
            android:layout_height="50dip"
            android:hint="请输入联系人电话号码" />

        <Button
            android:id="@+id/selectContact2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="第二个联系人" />
    </RelativeLayout>

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:hint="请输短信内容"
        android:inputType="textMultiLine"
        android:minLines="5" />

    <Button
        android:id="@+id/sendsms"
        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_selectcontcat"
        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_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="姓名"
        android:textColor="#000000"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_contact_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="电话"
        android:textColor="#990000"
        android:textSize="16sp" />

</LinearLayout>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值