開啟新的activity獲取它的返回值

来源:http://www.cnblogs.com/zhongyinghe/archive/2016/03/18/5290117.html
-Advertisement-
Play Games

1、開始界面 2、開啟新的activity代碼 3、獲取聯繫人 1)清單文件 <uses-permission android:name="android.permission.READ_CONTACTS"/>//許可權 2)通過內容提供者獲取聯繫人 4、設置聯繫人進Listview java代碼:


1、開始界面

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

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <EditText
            android:id="@+id/et_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="請輸入聯繫人" />
        <Button 
            android:onClick="click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="聯繫人"
            />
    </LinearLayout>
     <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <EditText
            android:id="@+id/et_number2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="請輸入聯繫人" />
        <Button 
            android:onClick="click2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="聯繫人2"
            />
    </LinearLayout>

</LinearLayout>

2、開啟新的activity代碼

 1 package com.example.smssender;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.EditText;
 9 
10 public class MainActivity extends Activity {
11 
12     private EditText et_number;
13     private EditText et_number2;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         et_number = (EditText) findViewById(R.id.et_number);
19         et_number2 = (EditText) findViewById(R.id.et_number2);
20     }
21 
22     public void click(View view){
23         Intent intent = new Intent(this, ContactActivity.class);
24         //startActivity(intent);
25         //請求碼的作用是區別是誰發起的請求
26         startActivityForResult(intent, 1);
27     }
28     
29     public void click2(View view){
30         Intent intent = new Intent(this, ContactActivity.class);
31         //startActivity(intent);
32         //請求碼的作用是區別是誰發起的請求
33         startActivityForResult(intent, 2);
34     }
35     
36     
37     @Override
38     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
39         // TODO Auto-generated method stub
40         super.onActivityResult(requestCode, resultCode, data);
41         if(data != null){
42             String number = data.getStringExtra("number");
43             if(requestCode == 1){
44                 et_number.setText(number);
45             }else{
46                 et_number2.setText(number);
47             }
48         }
49     }
50 
51 }

3、獲取聯繫人

      1)清單文件

  <uses-permission android:name="android.permission.READ_CONTACTS"/>//許可權

      2)通過內容提供者獲取聯繫人

       

 1 package com.example.smssender;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.content.ContentResolver;
 7 import android.content.Context;
 8 import android.database.Cursor;
 9 import android.net.Uri;
10 
11 public class ContactService {
12     public static List<contactInfo> getContactAll(Context context){
13         List<contactInfo> infos = new ArrayList<contactInfo>();
14         //通過內容提供者獲取聯繫人
15         ContentResolver resolver = context.getContentResolver();
16         Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
17         Uri dataUri = Uri.parse("content://com.android.contacts/data");
18         Cursor cursor = resolver.query(uri, null, null, null, null);
19         while(cursor.moveToNext()){
20             String id = cursor.getString(cursor.getColumnIndex("contact_id"));
21             Cursor datacursor = resolver.query(dataUri, null, "raw_contact_id=?", new String[]{id}, null);
22             contactInfo info = new contactInfo();
23             while(datacursor.moveToNext()){
24                 String data1 = datacursor.getString(datacursor.getColumnIndex("data1"));
25                 String mimetype = datacursor.getString(datacursor.getColumnIndex("mimetype"));
26                 if("vnd.android.cursor.item/name".equals(mimetype)){
27                     info.setUsername(data1);
28                 }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
29                     info.setNumber(data1);
30                 }
31             }
32             
33             infos.add(info);
34             
35         }
36         return infos;
37     }
38 }

4、設置聯繫人進Listview

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <ListView
 8         android:layout_width="match_parent"
 9         android:layout_height="match_parent" 
10         android:id="@+id/lv_contact"
11         ></ListView>
12 
13 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <TextView 
 8         android:id="@+id/et_username"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         />
12     <TextView 
13         android:id="@+id/et_number"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         />
17 
18 </LinearLayout>

java代碼:

 1 package com.example.smssender;
 2 
 3 import java.util.List;
 4 
 5 import android.app.Activity;
 6 import android.content.ContentResolver;
 7 import android.content.Intent;
 8 import android.database.Cursor;
 9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.AdapterView;
14 import android.widget.AdapterView.OnItemClickListener;
15 import android.widget.BaseAdapter;
16 import android.widget.ListView;
17 import android.widget.TextView;
18 
19 public class ContactActivity extends Activity {
20 
21     private ListView lv_contact;
22     private List<contactInfo> infos = null;
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         
26         // TODO Auto-generated method stub
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_contact);
29         
30         infos = ContactService.getContactAll(this);
31         
32         lv_contact = (ListView)findViewById(R.id.lv_contact);
33         lv_contact.setAdapter(new ContactAdapter());
34         
35         lv_contact.setOnItemClickListener(new OnItemClickListener() {
36 
37             @Override
38             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
39                     long arg3) {
40                 // TODO Auto-generated method stub
41                 contactInfo info = infos.get(arg2);
42                 String number = info.getNumber();
43                 Intent data = new Intent();
44                 data.putExtra("number", number);
45                 setResult(0, data);
46                 //關閉當前activity,把數據傳回給它的激活者
47                 finish();
48                 
49             }
50             
51         });
52     }
53     
54     private class ContactAdapter extends BaseAdapter{
55 
56         @Override
57         public int getCount() {
58             // TODO Auto-generated method stub
59             return infos.size();
60         }
61 
62         @Override
63         public Object getItem(int arg0) {
64             // TODO Auto-generated method stub
65             return null;
66         }
67 
68         @Override
69         public long getItemId(int arg0) {
70             // TODO Auto-generated method stub
71             return 0;
72         }
73 
74         @Override
75         public View getView(int arg0, View arg1, ViewGroup arg2) {
76             // TODO Auto-generated method stub
77             contactInfo info = infos.get(arg0);
78             View view = View.inflate(ContactActivity.this, R.layout.contact_item, null);
79             TextView et_username = (TextView)view.findViewById(R.id.et_username);
80             et_username.setText(info.getUsername());
81             
82             TextView et_number = (TextView)view.findViewById(R.id.et_number);
83             et_number.setText(info.getNumber());        
84             
85             return view;
86         }
87         
88     }
89 
90 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Direction: up, down, left, right scrollamount:滾動速度,單位像素 loop: -1無限迴圈
  • 環境描述 前端:jsp 後端:SpringMVC Controller 儘管jsp頁面已設置了pageEncoding: 然後在控制器中,讀取到的對應參數如果含有中文,則出現亂碼,例如: public ModelAndView search(@RequestParam("keyword") Stri...
  • 基於 Material Design 的 BiliBili 第三方 Android 客戶端,我們知道這個APP目前比較流行,所以大家也比較喜歡模仿,需要的參考一下 文檔共用 : https://drive.google.com/folderview?id=0B5Izr6QMl6WhflNwY3MyZ
  • 高仿餓了麽界面效果,動畫效果還是不錯滴,分享給大家一下。 源碼下載:http://code.662p.com/list/11_1.html <ignore_js_op> 詳細說明:http://android.662p.com/thread-6472-1-1.html
  • 一,工程圖。 二,代碼。 AppDelegate.h AppDelegate.m RootViewController.h RootViewController.m
  • 這一次給大家帶來的是ios中點擊背景如何收鍵盤 直接上圖: 先創建一個這樣的頁面,把兩個文本框進行連線: 其實,很簡單,視圖控制器有一個view屬性,是從UIViewController繼承來的。這個view屬性對應的nib文件中的View。使用界面構造器,可以更改view所指向的對象所屬的類。將它
  • 功能實現:一個EditView 一個撥打按鈕,輸入號碼跳轉到撥號界面 界面佈局:activity_call.xml CallActivity的Create方法 增加撥打電話的許可權:AndroidManifest.xml 至此可以實現撥號功能。
  • 分類:C#、Android、VS2015 創建日期:2016-03-18 一、卸載原來安裝的Xamarin for VS 4.0.0.1717版 下麵是Xamarin for VS發佈的版本簡介: ……更早的版本(3.11.XXX )略 2015年11月發佈:Xamarin for VS 4.0.0...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...