Activity的創建步驟+Activity之間的數據傳遞+案例(人品測試器)

Android的四大組件:

1、activity(多層界面運用)
2、廣播接收者
3、服務
4、內容提供者

此外,我這裏還會說道
5、多媒體編程(圖形、聲音、視頻)
6、Fragment+動畫
7、SVN 版本控制

1.多界面應用創建的步驟

  1. 在Layout裏定義一個新佈局佈局
    這裏寫圖片描述
  2. 在src文件裏新創建一個activity,指定顯示的佈局
    這裏寫圖片描述
  3. 在清單文件AndroidManifest裏註冊activity。
    這裏寫圖片描述

2.多層界面之間的數據傳遞

  • 簡單數據

    1. 封裝數據

      Intent intent = new Intent(this , xxx.class);   //指的是從本頁面的數據轉到xxx.class頁面裏
      
      intent.putExtra("name","zhangsan"); //這裏可以打包成bundle,
      
      startActivity(intent);
      
    2. 取數據

      Intent intent = getIntent();
      
      String name = intent.getStringExtra("name");      //是上面頁的數據名稱name
      
  • 對象數據(傳送對象的時候)

    1. 實現Serializable接口

代碼:

package com.itheima.rp;

import java.io.Serializable;

public class Student implements Serializable{


    String name ;
    int gender;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getGender() {
        return gender;
    }
    public void setGender(int gender) {
        this.gender = gender;
    }
    public Student(String name, int gender) {
        super();
        this.name = name;
        this.gender = gender;
    }



}
  1. 實現Parcelable接口

    > 注意:要想傳遞集合這種數據類型,並且集合裏面也是包裝對象的,那麼這個對象類,只能去實現parcelable , Serializable是辦不到的。
    

代碼如下:

package com.itheima.rp;

import java.io.Serializable;

import android.os.Parcel;
import android.os.Parcelable;

public class Teacher implements Parcelable{


    String name ;
    int gender;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getGender() {
        return gender;
    }
    public void setGender(int gender) {
        this.gender = gender;
    }
    public Teacher(String name, int gender) {
        super();
        this.name = name;
        this.gender = gender;
    }


   public int describeContents() {
 return 0;
}

 public void writeToParcel(Parcel out, int flags) {
     //把數據寫到包裏面,或者叫做通道里面 讀寫順序必須要求一致 。 否則數據將會錯亂。
 out.writeString(name);
 out.writeInt(gender);
 }

 public static final Parcelable.Creator<Teacher> CREATOR
 = new Parcelable.Creator<Teacher>() {
 public Teacher createFromParcel(Parcel in) {
 return new Teacher(in);
 }

 public Teacher[] newArray(int size) {
 return new Teacher[size];
 }
 };

 private Teacher(Parcel in) {

     //從包裏面讀取數據
 name = in.readString();
 gender = in.readInt();
 }              
}

3.案例—人品計算器

如圖:

首先是在Layout創建三個佈局

第一個先是Logo佈局,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"

/>


</RelativeLayout>

第二個是主頁面的佈局,代碼如下:

<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"
    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=".MainActivity" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:src="@drawable/logo" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="請選擇性別:" />

    <RadioGroup
        android:id="@+id/rg_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />

        <RadioButton
            android:id="@+id/rb_unkown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="未知" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="請輸入姓名" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="calc"
            android:text="測算" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="測算的結果" />

</LinearLayout>

第三個是測試結果頁面的佈局,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView 
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_result"
        />


</RelativeLayout>

接着是在src創建三個與佈局相對應的activity

第一個先是Logo佈局的activity,代碼如下:

package com.itheima.rp;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class LogoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logo);



        new Thread(){
            public void run() {

//              sleep(3000);

                SystemClock.sleep(3000);

                Intent intent = new Intent(LogoActivity.this , MainActivity.class);
                startActivity(intent);

                //關閉當前界面

                finish();
            };
        }.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

第二個是主頁面的佈局對應的activity,代碼如下:

package com.itheima.rp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText et_name;
    TextView tv_result ;
    RadioGroup rg_gender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_name = (EditText) findViewById(R.id.et_name);

        tv_result =  (TextView) findViewById(R.id.tv_result);

        rg_gender =  (RadioGroup) findViewById(R.id.rg_gender);
    }

    //計算人品值
    public void calc(View v){
        String name = et_name.getText().toString();

        if(TextUtils.isEmpty(name)){
            Toast.makeText(this, "姓名不能爲空", 0).show();
            return ;
        }


        //獲取到了選中的性別id
        int id = rg_gender.getCheckedRadioButtonId();


        //tv_result.setText(str);
        // intent -- 意圖  就是連接四大組件的一座橋樑或者是紐帶。
        Intent intent = new Intent(this , ResultActivity.class);

    //  map.put(key ,value);

        //相當於是往橋上放了數據,數據的key -- str
//      intent.putExtra("str", str);

        //People people = new People(name , gender);



  /* */     intent .putExtra("name", name);

        intent.putExtra("gender", id);


        startActivity(intent);
    }

}

第三個是測算結果頁面的佈局對應的activity,代碼如下:

package com.itheima.rp;

import java.io.UnsupportedEncodingException;

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

public class ResultActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_result);

        TextView tv_result = (TextView)findViewById(R.id.tv_result);

        //這裏是獲取了上一個界面啓動的intent
        Intent intent = getIntent();
        //獲取上一個界面封裝的數據
//      String result = intent.getStringExtra("str");

        String name = intent.getStringExtra("name");
        int id = intent.getIntExtra("gender", 0);

        byte [] bytes= null;
        try {
            if(id == R.id.rb_male){ //男性
                bytes = name.getBytes(); //UTF-8
            }else if(id == R.id.rb_female){ // 女性
                bytes = name.getBytes("GBK");
            }else{ //未知性別
                bytes = name.getBytes("ISO-8859-1");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }



        int result = 0 ; 

        for (byte b : bytes) {
//          int result = b & 0xff;  //提升類型成 int 
            result += Math.abs(b&0xfff);
        }

        result = result % 100;

        Toast.makeText(this, "人品值是:"+result, 0).show();

        String str = null;

        if(result > 80){
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:天啊,你的人品值爆表";
        }else if(result > 70){
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:你的人品很不錯";
        }else if(result > 50){
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:你的人品一般般";
        }else if(result > 30){
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:你從3歲的時候,估計偷看鄰居大媽洗澡了";
        }else if(result > 10){
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:偷雞摸狗的事情經查幹吧.";
        }else{
            str = "姓名:"+name+"\r\n人品值:"+result+"\r\n評價:對不起,我不應該跟你談人品.";

        }


        tv_result.setText(str);

    }
}

最後是在清單文件註冊activity

清單文件的代碼如下:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar" >



        <activity

            android:name="com.itheima.rp.LogoActivity"

            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="com.itheima.rp.MainActivity"
            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="com.itheima.rp.ResultActivity"></activity>

    </application>

</manifest>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章