android 案例:从另一个activity选择信息并获取返回值

主窗口:

package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends Activity {
    //两个联系人号码填写框
    private EditText phoneEditText;
    private EditText phoneEditText2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        phoneEditText = (EditText) findViewById(R.id.et_phone);
        phoneEditText2 = (EditText) findViewById(R.id.et_phone2);
    }

    /**
     * 点击“选择联系人”进入选择第一个联系人的界面
     */
    public void selectContact(View view) {
        Intent intent = new Intent(this, SelectContactActivity.class);
        this.startActivityForResult(intent, 1);
    }

    /**
     * 点击“选择联系人”进入选择第二个联系人的界面
     */
    public void selectContact2(View view) {
        Intent intent = new Intent(this, SelectContactActivity.class);
        this.startActivityForResult(intent, 2);
    }

    /**
     * 当第二个activity选择完成,在退出的时候会调用这个方法
     * @param requestCode 请求码
     * @param resultCode 结果码
     * @param data 包含用户选择的值的intent对象
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String phone = data.getStringExtra("phone");
            if (requestCode == 1) {
                phoneEditText.setText(phone);
            } else if (requestCode == 2) {
                phoneEditText2.setText(phone);
            }
        }
    }
}

第二个窗口(选择联系人窗口):

package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 14-1-1.
 */
public class SelectContactActivity extends Activity {
    private ListView listView;
    private List<Person> personList = new ArrayList<Person>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_contact_layout);

        this.listView = (ListView) findViewById(R.id.lv_contact);
        initListData();//初始化测试数据
        this.listView.setAdapter(adapter);
        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //创建一个intent用于向上一个activity传递数据
                Intent intent = new Intent();
                Person person = personList.get(i);
                intent.putExtra("phone", person.getPhone());
                setResult(0, intent);
                finish();
            }
        });
    }

    private ListAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return personList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Person currentPerson  = personList.get(i);
            View itemView = View.inflate(getApplicationContext(), R.layout.contact_item, null);
            TextView nameView = (TextView) itemView.findViewById(R.id.tv_name);
            TextView phoneView = (TextView) itemView.findViewById(R.id.tv_phone);
            nameView.setText(currentPerson.getName());
            phoneView.setText(currentPerson.getPhone());
            return itemView;
        }
    };

    /**
     * 为程序提供测试数据
     */
    private void initListData() {
        Person person = new Person();
        person.setName("张三");
        person.setPhone("127836874");
        this.personList.add(person);

        person = new Person();
        person.setName("李四");
        person.setPhone("675698436");
        this.personList.add(person);

        person = new Person();
        person.setName("王五");
        person.setPhone("079456897");
        this.personList.add(person);
    }
}

主窗口xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

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

        <TextView android:text="@string/tv_select_contact"
                  android:layout_alignParentLeft="true"
                  android:paddingLeft="10dp"
                  android:paddingTop="10dp"
                  android:textSize="19sp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>

        <Button android:text="@string/button_select"
                android:onClick="selectContact"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </RelativeLayout>

    <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/>

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

        <TextView android:text="@string/tv_select_contact2"
                  android:layout_alignParentLeft="true"
                  android:paddingLeft="10dp"
                  android:paddingTop="10dp"
                  android:textSize="19sp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>

        <Button android:text="@string/button_select"
                android:onClick="selectContact2"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    </RelativeLayout>

    <EditText
            android:id="@+id/et_phone2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/>

</LinearLayout>

选择联系人窗口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">

    <ListView
            android:id="@+id/lv_contact"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </ListView>
</LinearLayout>

listView的item布局xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:id="@+id/tv_name"
            android:text="姓名"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/tv_phone"
            android:text="电话号码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

 string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">案例:从另一个activity选择并返回值</string>
    <string name="button_select">选择联系人</string>
    <string name="tv_select_contact">输入或选择号码1:</string>
    <string name="tv_select_contact2">输入或选择号码2</string>
</resources>

 

posted @ 2014-01-01 22:29  无忧之路  阅读(977)  评论(0编辑  收藏  举报
无忧之路