[android] 开启新的activity获取他的返回值
应用场景:打开一个新的activity,在这个activity上获取数据,返回给打开它的界面
短信发送时,可以直接选择系统联系人
界面布局是一个线性布局,里面右侧选择联系人在EditText的右上,因此使用相对布局对输入框进行包裹,按钮使用android:layout_alignParentRight=”true”处理
下面的内容有多行,使用 属性android:inputType=”textMultiLine” 属性android:minLines=”5”
我们使用hvg的屏幕进行预览
打开一个新的界面展示系统联系人,采用ListView控件实现列表,继承BaseAdapter来实现适配器,通过ContentProvider读取系统的联系人。
MainActivity(主界面)
package com.tsh.gaojisms; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity {
private EditText et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number=(EditText) findViewById(R.id.et_number);
}
/**
* 选择联系人
* @param v
*/
public void selectContacts(View v){
Intent intent=new Intent(this,SelectContactActivity.class);
startActivityForResult(intent, 0);
}
/**
* 接受返回的结果
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String number=data.getStringExtra("number");
et_number.setText(number);
}
}
SelectContactActivity(联系人界面)
package com.tsh.gaojisms; import java.util.List; import com.tsh.gaojisms.domain.ContactInfo;
import com.tsh.gaojisms.service.ContactInfosService; 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; public class SelectContactActivity extends Activity {
private ListView lv_contacts;
public List<ContactInfo> contactInfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_contact);
lv_contacts=(ListView) findViewById(R.id.lv_contacts);
//获取数据
contactInfos=ContactInfosService.getContactInfos(this);
lv_contacts.setAdapter(new MyAdapter());
//设置点击
lv_contacts.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 intent=new Intent();
intent.putExtra("number", number);
setResult(0,intent);
finish();
}
});
}
/*
* 适配器
*/
private class MyAdapter extends BaseAdapter { @Override
public int getCount() {
// TODO Auto-generated method stub
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) {
View view=View.inflate(SelectContactActivity.this, R.layout.contact_item, null);
TextView tv_name=(TextView) view.findViewById(R.id.tv_name);
TextView tv_number=(TextView) view.findViewById(R.id.tv_number);
String name=contactInfos.get(position).getName();
String number=contactInfos.get(position).getNumber();
tv_name.setText(name);
tv_number.setText(number);
return view;
}}
}
ContactInfoService(获取联系人信息Service)
package com.tsh.gaojisms.service; 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.tsh.gaojisms.domain.ContactInfo; public class ContactInfosService {
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, null,
"raw_contact_id=?", new String[] { id }, null);
ContactInfo info = new ContactInfo();
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(dataCursor
.getColumnIndex("data1"));
String mimetype = dataCursor.getString(dataCursor
.getColumnIndex("mimetype"));
if (mimetype.equals("vnd.android.cursor.item/name")) {
info.setName(data1);
} else if (mimetype
.equals("vnd.android.cursor.item/phone_v2")) {
info.setNumber(data1);
}
}
contactInfos.add(info);
} }
return contactInfos;
}
}
ContactInfo(联系人信息bean)
package com.tsh.gaojisms.domain;
/**
* 联系人业务bean
* @author taoshihan
*
*/
public class ContactInfo {
private String name;
private String number;
public ContactInfo() {
}
public ContactInfo(String name, String number) {
super();
this.name = name;
this.number = number;
}
@Override
public String toString() {
return "ContactInfo [name=" + name + ", number=" + 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;
}
}
activity_main.xml(主布局界面)
<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="${relativePackage}.${activityClass}" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <EditText
android:id="@+id/et_number"
android:layout_marginTop="17dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入号码" /> <Button
android:onClick="selectContacts"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="短信内容"
android:inputType="textMultiLine"
android:minLines="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/> </LinearLayout>
activity_select_contact.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" > <ListView
android:id="@+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
contact_item.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_name"
android:text="用户名"
android:textSize="18sp"
android:textColor="#ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_number"
android:text="电话号码"
android:textSize="12sp"
android:textColor="#80000000"/>
</LinearLayout>
这里开启activity需要用到一个新的api,startActivityForResult(intent,requestCode),开启一个新的activity并且获取这个activity执行完毕后返回的结果,参数:Intent对象,int类型请求码,此时用不到给个0
当新开启的activity关闭的时候,会调用onActivityResult()方法。传递过来的参数里面有个Intent对象,通过这个Intent对象获取到数据,展示到界面上
给ListView条目设置点击事件,调用ListView对象的setOnItemClickListener(listener)方法,参数:OnitemClickListener对象,它是一个接口类型,直接new这个接口,实现以下onItemClick(parent,view,id)方法,如果不知道函数的参数,我们可以采用断点调试的方法,查看一下参数代表的是什么值,调用ContactInfos对象的get(position)方法,得到ConatctInfo对象,调用ContactInfo对象的getNumber()方法,得到电话号码
传递数据给调用它的activity,调用setResult(requestCode,data)方法,参数:请求码,Intent对象,点击完成之后调用Activity对象的finish()方法


[android] 开启新的activity获取他的返回值的更多相关文章
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...
- 开启新的activity获取它的返回值
1.开始界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 关于Android 打开新的Activity 虚拟键盘的弹出与不弹出
关于Android 打开新的Activity 虚拟键盘的弹出与不弹出 打开Activity 时 在相应的情况 弹出虚拟键盘 或者 隐藏虚拟键盘 会给用户非常好的用户体验 , 实现起来也比较简单 只需 ...
- python asyncio 获取协程返回值和使用callback
1. 获取协程返回值,实质就是future中的task import asyncioimport timeasync def get_html(url): print("start get ...
- 统计文件种类数+获取子shell返回值的其它方法
前言 只是作为一个shell的小小练习和日常统计用,瞎折腾的过程中也是摸到了获取子shell返回值的几种方法: 肯定还有别的方法,跟进程间的通信相关,希望你能提出建议和补充,谢谢~ 完整程序: #! ...
- web3调用call()方法获取不到返回值
一.web3的call()获取不到返回值问题和解决方法 在彩票小合约中,遇到一个问题:合约中 有两个方法 第一个返回一个账户地址,没有使用到当前方法调用者信息: 第二个使用到了当前方法调用者信息 在w ...
- 利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程
1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器 ...
- python使用threading获取线程函数返回值的实现方法
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
- Android -- 在一个Activity开启另一个Activity 并 获取他的返回值。
1. 视图示例, 按选择弹出 2界面, 选择选项 回显到1 2. 示例代码 MainActivity.java, 第一个activity public class MainActivity e ...
随机推荐
- 在使用可变数组过程中遇到*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'问题
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFD ...
- Akka-CQRS(0)- 基于akka-cluster的读写分离框架,构建gRPC移动应用后端架构
上一篇我们讨论了akka-cluster的分片(sharding)技术.在提供的例子中感觉到akka这样的分布式系统工具特别适合支持大量的带有内置状态的,相对独立完整的程序在集群节点上分布运算.这里重 ...
- wordpress背景添加跟随鼠标动态线条特效
今天看到别人的博客,在鼠标移动背景时会出现线条动态特效 感觉挺有意思的,还有另一种,在背景点击时会跳出字符特意去找了方法,以为需要添加代码的,结果只要安装个插件就可以了,所以说wordpress就是方 ...
- 第53节:Java当中的IO流(上)
Java当中的IO流 在Java中,字符串string可以用来操作文本数据内容,字符串缓冲区是什么呢?其实就是个容器,也是用来存储很多的数据类型的字符串,基本数据类型包装类的出现可以用来解决字符串和基 ...
- 几个java小例子
比较两个字符串的值: /*------------------------比较两个字符串的值----------------------*/ String st1="hello"; ...
- 机器学习入门11 - 逻辑回归 (Logistic Regression)
原文链接:https://developers.google.com/machine-learning/crash-course/logistic-regression/ 逻辑回归会生成一个介于 0 ...
- Python - References
01 - Python文档 Python:https://www.python.org/ Documentation:https://docs.python.org/ Standard Library ...
- Emmet/Zen Coding 快速入门说明
快速参考 以下是支持的特性: ele creates an HTML element tag 展开一个HTML元素标签 # creates an id attribute 作用于元素标签,展开一个id ...
- Hystrix 停止开发。。。Spring Cloud 何去何从?
栈长得到消息,Hystrix 停止开发了... 大家如果有对 Hystrix 不清楚的,请看下这篇文章:分布式服务防雪崩熔断器,Hystrix理论+实战. 来看下 Hystrix 停止开发官宣: ht ...
- Android Studio 更新同步Gradle错误解决方法
1.在https://services.gradle.org/distributions/下载对应的gradle的zip包,对应方法见gradle-wrapper.properties文件中的: di ...