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

android之bundle传递数据--两个activities之间

时间:2014-12-07 06:51:32      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:activities   android   layout   

登陆页面布局:layout/activity_login.xml

<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:stretchColumns="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TableRow >
        <TextView 
        android:text="用户名:"/>
<EditText 
   android:id="@+id/etUser"
   android:hint="请输入1-10个字符"
    />
    </TableRow>
     <TableRow >
        <TextView 
        android:text="密码:"/>
<EditText 
   android:id="@+id/etPassword"
   android:hint="请输入1-10个字符"
    />
    </TableRow>
    
</TableLayout>
    <TableLayout 
    android:stretchColumns="0,2,4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TableRow >
       <Button 
           android:visibility="invisible"/>
       <Button 
           android:id="@+id/login"
           android:text="登陆"
          android:drawableLeft="@drawable/login32x32"
          android:background="@drawable/btn_bg"/>
       <Button 
           android:visibility="invisible"/>
       <Button 
           android:id="@+id/exit" 
           android:text="退出"
           android:drawableLeft="@drawable/exit32x32"
          android:background="@drawable/btn_bg"
           />
       <Button 
           android:visibility="invisible"/>
    </TableRow>
    
</TableLayout>
</LinearLayout>

登陆java类:

package com.litsoft.day04;


import com.litsoft.entity.User;


import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends ActionBarActivity {
private EditText etUser,etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
setListener();
}


private void setListener() {
// TODO Auto-generated method stub
findViewById(R.id.login).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String userText = etUser.getText().toString();
if(TextUtils.isEmpty(userText)){
etUser.setError("用户名不能为空");
return;
}
String passwordText = etPassword.getText().toString();
if(TextUtils.isEmpty(passwordText)){
etUser.setError("密码不能为空");
return;
}
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
Bundle bundle = new Bundle();
User user = new User(userText,passwordText);
bundle.putSerializable("user", user);
intent.putExtras(bundle);
startActivity(intent);
}
});
findViewById(R.id.exit).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
finish();
}
});
}


private void init() {
// TODO Auto-generated method stub
this.etUser = (EditText) findViewById(R.id.etUser);
this.etPassword = (EditText) findViewById(R.id.etPassword);
}



}

跳转页面布局:layout/activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.litsoft.day04.MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>

跳转java代码:

package com.litsoft.day04;


import com.litsoft.entity.User;


import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle data = intent.getExtras();
User user = (User) data.getSerializable("user");
Toast.makeText(this, "注册信息:"+user, 500000).show();
setContentView(R.layout.activity_main);
}
}

User类:

package com.litsoft.entity;


import java.io.Serializable;


public class User implements Serializable{
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name:"+this.name+"password:"+this.password;
}
}

菜单列表:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.litsoft.day04"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
        </activity>
    </application>


</manifest>

效果:

bubuko.com,布布扣

bubuko.com,布布扣

本文出自 “追随心的彼岸” 博客,谢绝转载!

android之bundle传递数据--两个activities之间

标签:activities   android   layout   

原文地址:http://qa962839575.blog.51cto.com/6819179/1587125

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