码迷,mamicode.com
首页 > 移动开发 > 详细

Activity之间的数据传递-android学习之旅(四十七)

时间:2015-06-23 10:19:46      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:android   activity   

activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的

使用Bundle传递数据

Intent使用Bundle在activity之间传递数据。
技术分享
技术分享
技术分享

代码示例

dto类person

package peng.liu.test;

import java.io.Serializable;

/**
 * Created by lplp on 2015/6/22.
 */
public class Person implements Serializable{
    private String name;
    private String passwd;
    private String sex;
    public Person(String name,String passwd,String male){
        this.name = name;
        this.passwd = passwd;
        this.sex = male;
    }
    public String getName(){
        return this.name;
    }
    public String getpasswd(){
        return this.passwd;
    }
    public String getSex(){
        return this.sex;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setPasswd(String passwd){
        this.passwd = passwd;
    }
    public void  setSex(String sex){
        this.sex = sex;
    }
}
package peng.liu.test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnSend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText name = (EditText) findViewById(R.id.name);
                EditText passwd = (EditText) findViewById(R.id.passwd);
                RadioButton male = (RadioButton) findViewById(R.id.male);
                String gender = male.isChecked() ? "男":"女";
                Person p = new Person(name.getText().toString(),passwd.getText().toString(),gender);
                Bundle data = new Bundle();
                data.putSerializable("person",p);
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(data);
startActivity(intent);
            }
        });
    }
}
package peng.liu.test;

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


public class MainActivity2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cell);
        Intent intent = getIntent();
        Person p = (Person) intent.getSerializableExtra("person");
        TextView name = (TextView) findViewById(R.id.name);
        TextView passwd = (TextView) findViewById(R.id.passwd);
        TextView sex = (TextView) findViewById(R.id.sex);
        name.setText(p.getName());
        passwd.setText(p.getpasswd());
        sex.setText(p.getSex());
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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"
    >
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="请输入您的注册信息"
            android:textSize="20sp"/>
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="16sp"/>
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:hint="输入您的注册名字"
                android:selectAllOnFocus="true"/>
        </TableRow>
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="密码:"
                android:textSize="16sp"/>
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/passwd"
                android:password="true"
                android:selectAllOnFocus="true"/>
        </TableRow>
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="密码:"
                android:textSize="16sp"/>
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/male"
                    android:textSize="16sp"
                    android:text="男"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/female"
                    android:textSize="16sp"
                    android:text="女"/>
                </RadioGroup>
        </TableRow>
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnSend"
            android:text="注册"
            android:textSize="16sp"/>
        </TableLayout>
</LinearLayout>
<?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="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/name"
    android:textSize="18sp"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/passwd"
        android:textSize="18sp"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sex"
        android:textSize="18sp"/>

</LinearLayout>

启动第二个Activity并返回结果

技术分享

代码示例

package peng.liu.test;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Text;


public class MainActivity2 extends ExpandableListActivity {
    private String[] provinces = new String[]{"shandong","shandong","shandong","shandong"};
    private String[][] citys = new String[][]{
            {"xian","xian","xian","xian"},
            {"xian","xian","xian","xian"},
            {"xian","xian","xian","xian"},
            {"xian","xian","xian","xian"}
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cell);
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            @Override
            public int getGroupCount() {
                return provinces.length;
            }

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

            @Override
            public Object getGroup(int i) {
                return citys[i].length;
            }

            @Override
            public Object getChild(int i, int i2) {
                return citys[i][i2];
            }

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

            @Override
            public long getChildId(int i, int i2) {
                return i2;
            }
            public TextView getTextView(){
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
                TextView textView = new TextView(MainActivity2.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
                textView.setPadding(36,0,0,0);
                textView.setTextSize(20);
                return textView;
            };
            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
                LinearLayout ll = new LinearLayout(MainActivity2.this);
                ll.setOrientation(LinearLayout.VERTICAL);
                ImageView image = new ImageView(MainActivity2.this);
                ll.addView(image);
                TextView text = getTextView();
                text.setText(getGroup(i).toString());
                ll.addView(text);
                return ll;
            }

            @Override
            public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
                TextView text = getTextView();
                text.setText(getChild(i,i2).toString());
                return text;
            }

            @Override
            public boolean isChildSelectable(int i, int i2) {
                return true;
            }
        };
        setListAdapter(adapter);
        getExpandableListView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
                Intent intent = getIntent();
                intent.putExtra("city",citys[i][i2]);
                MainActivity2.this.setResult(0,intent);
                MainActivity2.this.finish();
                return false;
            }
        });
    }
}
package peng.liu.test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends Activity{
    EditText city;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city = (EditText) findViewById(R.id.city);
        findViewById(R.id.btnCity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivityForResult(intent,0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bundle intent = data.getExtras();
        String city1 = intent.getString("city");
        city.setText(city1);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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"
    >
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/city"
        android:text="city"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCity"
        android:text="select"/>
</LinearLayout>
这里写代码片

Activity之间的数据传递-android学习之旅(四十七)

标签:android   activity   

原文地址:http://blog.csdn.net/lpjishu/article/details/46597769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!