加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Activity经典实例一:两个Activity传递数据和对象

(2012-11-18 21:38:30)
标签:

android4.1.2

cocos2d-x

iphone4s

linux

it

分类: 网络编程

1、概述:

Activity直接或者接地承了ContextContextWrapperContextThemeWrapper等基,因此Activity可以直接用它的方法。

建一个Activity需要实现某些方法,常的是实现onCreate(Bundle status)方法,方法将会在Activity被回,它setContentView(View view)方法来示要展示的View

一个Android用常常有多个Activity,但是只有一个作程序的入口,其他的Activity通常都由入口Activity、及其后者启

2Activity另一个Activity的方法:

startActivity(Intent intent):启其他Activity

startActivityForResult(Intent intent, int requestCode):以指定(requestCode)Activity,而且程序将会等到新启Activity果(通重写onActivityResult(...)方法来果)。

3、关Activity的方法:

 finish()束掉当前的Activity

 finishActivity(int requestCode)束以startActivityForResult()方法启Activity

4、使用BundleActivity数据:

  1)Intent:主要通Intent个信使,将需要交的数据放入即可。Intent提供了方法用于携数据,如:

  putExtras(Bundle data):向Intent中放入需要携的数据;

  2)Bundle:就是一个简单的数据包,Bundle象包含了多个方法来存入、取出数据,有:

  putXxx(String key, Xxx data):向Bundle放入IntLong等各种型的数据;

  putSerializable(String key, Serializable data):向Bundle放入一个可序列化的象;

  getXxx(String key):从Bundle取出IntLong等各种型的数据;

  getSerializable(String key):从Bundle取出一个可序列化的象。

5、开发实例:注册用信息

介:程序包含两个Activity,一个填写信息,另一个示注册果。

http://s4/mw690/5a6efa33g7b13b68c2733&690

http://s7/mw690/5a6efa33g7b13b69f6666&690

http://s2/mw690/5a6efa33gcec522f80b31&690

 

完整代

RegisterActivity.java源代

package com.shhuangpu.userlogin;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

 

import android.content.Intent;

 

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.Toast;

import android.view.Gravity;

 

public class LoginMainActivity extends Activity {

    private Button regButton;

    private EditText nameEdit;

    private EditText passwdEdit;

    private RadioButton maleButton;

    private Toast myToast;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login_main);

       

        regButton = (Button) findViewById(R.id.register_now);

        nameEdit = (EditText) findViewById(R.id.name_edit);

        passwdEdit = (EditText) findViewById(R.id.password_edit);

        maleButton = (RadioButton) findViewById(R.id.male_btn);

 

        regButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

 

                String name = nameEdit.getText().toString();

                String passwd = passwdEdit.getText().toString();

                String gender = maleButton.isChecked() ? "" : "";

 

                // 如果入信息不完整,不允注册。

                if (name.equals("") || passwd.equals("")) {

                    //Toast.makeText(LoginMainActivity.this, "填写完整的信息!", 3000).show();

                   

                //  myToast=Toast.makeText(getApplicationContext(),getText(R.string.mybutton2_text)+myeditText1.getText().toString(),Toast.LENGTH_LONG);

                //  myToast.setGravity(Gravity.CENTER, 0, 0);

                //  myToast.show();

   myToast = Toast.makeText(LoginMainActivity.this, "填写完整的信息!",Toast.LENGTH_LONG);

   myToast.setGravity(Gravity.CENTER,0,0);

   myToast.show();

                    }

                else {

                    // Person象,一个可序列化的象。

                    Person person = new Person(name, passwd, gender);

 

                    Bundle bundle = new Bundle();

                    bundle.putSerializable("person", person);

 

                    Intent intent = new Intent(LoginMainActivity.this,ResultActivity.class);

                    intent.putExtras(bundle);

                    // 另一个Activity

                    startActivity(intent);

                }

            }

        });

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.activity_login_main, menu);

        return true;

    }

 

}

ResultActivity.java源代

package com.shhuangpu.userlogin;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

 

import android.content.Intent;

 

import android.widget.TextView;

 

public class ResultActivity extends Activity {

    private TextView nameText;

    private TextView passwdText;

    private TextView genderText;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_result);

   

        nameText = (TextView) findViewById(R.id.name_text);

        passwdText = (TextView) findViewById(R.id.passwd_text);

        genderText = (TextView) findViewById(R.id.gender_text);

 

        // 取启动该ResultActivityIntent

        Intent intent = getIntent();

        // Intent所携的数据

        Bundle bundle = intent.getExtras();

        // bundle数据包中取出数据

        Person person = (Person) bundle.getSerializable("person");

 

        // 示注册

        nameText.setText("您的号:" + person.getName());

        passwdText.setText("您的密" + person.getPasswd());

        genderText.setText("您的性" + person.getGender());

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.activity_result, menu);

        return true;

    }

 

}

 

main.xml布局文件:

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

<</SPAN>TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >

 

    <</SPAN>TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/register_info_text"

        android:textSize="20sp" />

 

    <</SPAN>TableRow>

 

        <</SPAN>TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/user_name"

            android:textSize="16sp" />

 

        <</SPAN>EditText

            android:id="@+id/name_edit"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:ems="10"

            android:hint="@string/set_name"

            android:inputType="textPersonName"

            android:visibility="visible" >

 

            <</SPAN>requestFocus />

        </</SPAN>EditText>

 

    </</SPAN>TableRow>

 

    <</SPAN>TableRow>

 

        <</SPAN>TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/user_password"

            android:textSize="16sp" />

 

        <</SPAN>EditText

            android:id="@+id/password_edit"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:ems="10"

            android:hint="@string/set_password"

            android:inputType="textPassword" />

 

    </</SPAN>TableRow>

 

    <</SPAN>TableRow>

 

        <</SPAN>TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/user_sex"

            android:textSize="16sp" />

 

        <</SPAN>RadioGroup

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >

 

            <</SPAN>RadioButton

                android:id="@+id/male_btn"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/male_btn"

                android:checked="true"

                android:textSize="16sp" />

 

            <</SPAN>RadioButton

                android:id="@+id/female_btn"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/female_btn"

                android:textSize="16sp" />

        </</SPAN>RadioGroup>

    </</SPAN>TableRow>

 

    <</SPAN>Button

        android:id="@+id/register_now"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/register_btn"

        android:textSize="16sp" />

 

</</SPAN>TableLayout>

result.xml布局文件:

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

<</SPAN>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <</SPAN>TextView

        android:id="@+id/name_text"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp" />

 

    <</SPAN>TextView

        android:id="@+id/passwd_text"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp" />

 

    <</SPAN>TextView

        android:id="@+id/gender_text"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp" />

 

</</SPAN>LinearLayout>

可序列化的Person

package com.shhuangpu.userlogin;

 

import java.io.Serializable;

 

public class Person implements Serializable {

 

    private static final long serialVersionUID = 1L;

    private String name;

    private String passwd;

    private String gender;

 

    public Person(String name, String passwd, String gender) {

        this.name = name;

        this.passwd = passwd;

        this.gender = gender;

    }

 

    public String getName() {

        return name;

    }

 

    public String getPasswd() {

        return passwd;

    }

 

    public String getGender() {

        return gender;

    }

}

strings.xml

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

<</SPAN>resources>

 

    <</SPAN>string name="app_name">UserLogin</</SPAN>string>

   

    <</SPAN>string name="menu_settings">Settings</</SPAN>string>

    <</SPAN>string name="title_activity_result">ResultActivity</</SPAN>string>

    <</SPAN>string name="register_info_text">请输入您的注册信息</</SPAN>string>

    <</SPAN>string name="user_name">号:</</SPAN>string>

    <</SPAN>string name="set_name"></</SPAN>string>

    <</SPAN>string name="user_password"></</SPAN>string>

    <</SPAN>string name="set_password">入密</</SPAN>string>

    <</SPAN>string name="user_sex"></</SPAN>string>

    <</SPAN>string name="male_btn"></</SPAN>string>

    <</SPAN>string name="female_btn"></</SPAN>string>

    <</SPAN>string name="register_btn">注册</</SPAN>string>

</</SPAN>resources>

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有