Android学习第六天---Activity

定义两个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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名" />

      <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

      <TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="用户密码" />

    <EditText
        android:id="@+id/userpasswd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login" />


</LinearLayout>

第二个xml,实现页面跳转的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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="~~~~" />
    
 </RelativeLayout>

java文件中

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import cn.core.entity.Person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{

    EditText editText1,editText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editText1=(EditText)findViewById(R.id.username);
        editText2=(EditText)findViewById(R.id.userpasswd);
        Button button=(Button)findViewById(R.id.login);
        
        
        button.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        
        String username = editText1.getText().toString();
        String userpasswd = editText2.getText().toString();
        
        List list=new ArrayList();
        
        Person person1 = new Person(1, "肖方鑫", "男");
        list.add(person1);
        
        Intent intent = new Intent();
        intent.putExtra("name", username);
        intent.putExtra("password", userpasswd);
        
        intent.putExtra("boolean", false);
        intent.putExtra("int", 50);
        intent.putExtra("float", 50.12f);
        
        intent.putExtra("list", (Serializable)list);
                intent.setClass(MainActivity.this,SecondActivity.class);
        startActivity(intent);
        
        
    }



}
package cn.core.test;

import java.util.List;

import cn.core.entity.Person;

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

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seconda_activity);
        
        TextView textView=(TextView)findViewById(R.id.textView1);
        Intent intent=this.getIntent();
        
        String userName=intent.getStringExtra("name");
        String userPasswd=intent.getStringExtra("password");
        
//        intent.getStringArrayListExtra(name)
        
        List list=(List) intent.getSerializableExtra("list");
        
        String pName=((Person)list.get(0)).getpName();
        
        boolean flag=intent.getBooleanExtra("boolean", true);
        int num=intent.getIntExtra("int", -1);
        float f=intent.getFloatExtra("float", 3.15f);
        
        textView.setText(userName+"--"+userPasswd+"--"+flag+"--"+num+"--"+f+"--"+pName);
        
    }

}
package cn.core.entity;

import java.io.Serializable;

public class Person implements Serializable{
    private Integer pid;
    private String pName;
    private String pSex;
    public Person(Integer pid, String pName, String pSex) {
        super();
        this.pid = pid;
        this.pName = pName;
        this.pSex = pSex;
    }
    public Integer getPid() {
        return pid;
    }
    public void setPid(Integer pid) {
        this.pid = pid;
    }
    public String getpName() {
        return pName;
    }
    public void setpName(String pName) {
        this.pName = pName;
    }
    public String getpSex() {
        return pSex;
    }
    public void setpSex(String pSex) {
        this.pSex = pSex;
    }
    
}

 

posted @ 2013-03-11 23:43  小三小山  阅读(258)  评论(0编辑  收藏  举报