Android学习第六天---activity2

主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

<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 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();
        
        
        
        Intent intent = new Intent();
        
        Bundle bundle = new Bundle();
        bundle.putString("name", userName);
        bundle.putString("userpasswd",userPasswd);
        intent.putExtra("bundle", bundle);
        
        
        intent.setClass(MainActivity.this,SecondActivity.class);
        startActivity(intent);
        
        
    }



}

第二个页面的activity

import java.util.List;
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();

        Bundle bundle=intent.getBundleExtra("bundle");
        String userName=(String)bundle.get("name");
        String userPasswd=(String)bundle.get("userpasswd");
        
        textView.setText(userName+"~~~"+userPasswd);
        
    }

}

 

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