Android中startActivityForResult的简单例子

这是一个完整的例子,完整复制就能运行

原理 startActivityForResult(intent, 200);---->setResult(200, intent);----->onActivityResult(int requestCode, int resultCode, Intent data)

功能:换城市名称

效果

Android中startActivityForResult的简单例子_第1张图片Android中startActivityForResult的简单例子_第2张图片Android中startActivityForResult的简单例子_第3张图片

项目结构

Android中startActivityForResult的简单例子_第4张图片

界面:

activity_main.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


            android:id="@+id/find_xian"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="西安"
        android:textSize="20dp" 
        android:background="#66669999"
        android:clickable="true"
        />



activity_second.xml


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


            android:id="@+id/city_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6699FF"
         >
   




activity_city_item.xml


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#6699FF"
     >


            android:id="@+id/city_items"
        android:layout_width="fill_parent"
        android:layout_height="95dp"
        android:text="北京" 
        android:background="#6699FF"
        android:textColor="#FFFFFF"
        android:textSize="30dp"
        android:padding="12dp"
        
        />


    

java代码:

MainActivity

package com.zhh.android;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MainActivity extends Activity {
private TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.find_xian);
tv.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, 200);


}


});


}
// 改变城市名称返回的数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
tv.setText(data.getStringExtra("name").toString());
}


}



}//class

SecondActivity

package com.zhh.android;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;


public class SecondActivity extends Activity {
private ListView citylist;
List> data;


// 写个城市的集合


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
init();
// 得到数据
List> mydata = getdata();
// 绑定数据
SimpleAdapter adapter = new SimpleAdapter(this, mydata,
R.layout.activity_city_item, new String[] { "name" },
new int[] { R.id.city_items });
citylist.setAdapter(adapter);


citylist.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Map map = (Map) parent
.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("name", (String) map.get("name"));
setResult(200, intent);


finish();


}
});


}


// 自己造的数据
public List> getdata() {
data = new ArrayList>();

Map map1 = new HashMap();
map1.put("name", "西安");

Map map2 = new HashMap();
map2.put("name", "北京");

Map map3 = new HashMap();
map3.put("name", "上海");

Map map4 = new HashMap();
map4.put("name", "广州");

Map map5 = new HashMap();
map5.put("name", "深圳");

data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5);


return data;
}


private void init() {
citylist = (ListView) findViewById(R.id.city_listview);
}


}

主清单文件中注册activity

              android:name="com.zhh.android.MainActivity"
            android:label="@string/app_name" >
           
               


               
           

       
                    android:name="com.zhh.android.SecondActivity"
            >







你可能感兴趣的:(Activity)