Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 開啟新的activity獲取它的返回值,activity獲取返回值

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

編輯:關於android開發

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


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 }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved