Android移動應用開發之Activity
- 1、Activity的建立
- 2、Activity的生命周期
-
- (1)生命周期狀态
-
- ①啟動狀态
- ②運作狀态
- ③暫停狀态
- ④停止狀态
- ⑤銷毀狀态
- (2)生命周期方法
-
- ①onCreate方法
- ②onStart方法
- ③onResume方法
- ④onPause方法
- ⑤onStop方法
- ⑥onDestroy方法
- ⑦onRestart方法
- 3、Activity的啟動模式
-
- (1)Android中的任務棧
- (2)Activity的4中啟動模式
-
- ①standard模式
- ②singleTop模式
- ③singleTask模式
- ④singlestance模式
- 4、Activity之間的跳轉
-
- (1)Intent
-
- ①顯式意圖
- ②隐式意圖
- (2)實戰演練:打開浏覽器
- 5、Activity中的資料傳遞
-
- (1)資料傳遞
- (2)實戰演練:注冊使用者資訊
- (3)資料回傳
- (4)實戰演練:線上店鋪
1、Activity的建立
在程式包名處右鍵->New->Activity->Empty Activity
package edu.hzuapps.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
建立的Main2Activity繼承自AppCompatActivity,并且重寫了onCreate方法。在建立Main2Activity.java後,AndroidManifest.xml會生成
進行注冊。
2、Activity的生命周期
(1)生命周期狀态
Activity生命周期狀态是指一個Activity從建立到銷毀的全過程,Activity生命周期狀态有五中,分别是啟動狀态、運作狀态、暫停狀态、停止狀态和銷毀狀态,其中啟動狀态和銷毀狀态是過渡狀态,Activity不會在這兩個狀态停留。
①啟動狀态
Activity的啟動狀态很短暫,一般情況下,啟動後就會自己進入運作狀态。
②運作狀态
Activity此時處于螢幕最前端,可以與使用者進行互動,如單擊、輕按兩下、長按事件等。
③暫停狀态
使用者對Activity沒有操作時,它就處于暫停狀态,例如目前Activity上覆寫了一個透明或者非全屏的Activity時,被覆寫的Activity就處于暫停狀态。
④停止狀态
當Activituy完全不可見時就處于停滞狀态,但仍保留着目前狀态和成員資訊。
⑤銷毀狀态
Activity被清理出記憶體。
一個Activity從啟動到關閉,會依次執行onCreate->onStart->onResume->onPause->onStop->onDestroy,當Activity執行到onPause失去焦點時,重新回到前台執行onResume,如果程序被銷毀,則重新執行onCreate,當執行到onStop時Activity不可見則會重新執行onCreate方法。
(2)生命周期方法
①onCreate方法
onCreate方法通常用做一些初始化設定:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
②onStart方法
onStart方法在Activity即将可見時調用:
@Override
protected void onStart() {
super.onStart();
}
③onResume方法
onResume方法在Activity擷取焦點開始與使用者互動時調用:
protected void onResume() {
super.onResume();
}
④onPause方法
onPause方法在目前Activity被其他Activity覆寫或鎖屏時使用:
protected void onPause() {
super.onPause();
}
⑤onStop方法
onStop方法在Activity對使用者不可見時調用:
protected void onStop() {
super.onStop();
}
⑥onDestroy方法
onDestroy方法在Activity銷毀時調用:
protected void onDestroy() {
super.onDestroy();
}
⑦onRestart方法
onRestart方法在Activity從停止狀态再次啟動時調用:
protected void onRestart() {
super.onRestart();
}
使用如下代碼檢視更為直覺:
package edu.hzuapps.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivityLifeTime","調用onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.i("MainActivityLifeTime","調用onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i("MainActivityLifeTime","調用onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i("MainActivityLifeTime","調用onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i("MainActivityLifeTime","調用onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivityLifeTime","調用onDestroy()");
} @Override
protected void onRestart() {
super.onRestart();
Log.i("MainActivityLifeTime","調用onRestart()");
}
}
手機在切換橫豎屏時會首先銷毀Activity,AndroidManifest.xml中使用如下代碼可防止銷毀:
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
如果希望某個界面一直處于橫屏/豎屏,比如遊戲橫屏等等,
豎屏:
android:screenOrientation="portrait"
橫屏:
android:screenOrientation="landscape"
3、Activity的啟動模式
每啟動一個新的Activity就會覆寫在原Activity上,如果單擊傳回則最上面的Activity被銷毀,這是因為Android系統是通過任務棧的方式來管理Activity執行個體。
(1)Android中的任務棧
(2)Activity的4中啟動模式
①standard模式
預設模式,每啟動一個Activity就會在棧頂建立一個新的執行個體。
②singleTop模式
該模式會判斷要啟動的Activity執行個體是否位于棧頂,如果位于棧頂則直接複用,否則建立新的執行個體。
③singleTask模式
該模式在每次啟動Activity時系統都會首先檢查棧中是否存在目前Activity,如果存在則直接使用,并把目前Activity之上的所有執行個體全部出棧,否則會重新建立一個執行個體。
④singlestance模式
該模式會建立一個新的任務棧來管理Activity執行個體,無論從哪個任務棧中啟動該Activity,該執行個體在整個系統中隻有一個,這種模式是為了在不同程式中共享同一個Activity執行個體。這裡有兩種情況:一是要啟動的Activity不存在,則系統會先建立一個新的任務棧,然後再建立Activity執行個體。一種是要啟動的Activity已經存在,無論目前Activity位于哪個程式哪個任務棧中,系統會把Activity所在的任務棧轉移到前台,進而使Activity顯示,例如來電界面。
4、Activity之間的跳轉
在Android中,每個Activity就是一個界面,在界面進行跳轉,其實也就是Activity的跳轉,使用Intent元件。
(1)Intent
①顯式意圖
直接通過名稱開啟指定的目标元件:
Intent intent = new Intent(this, Activity2.class); //建立Intent對象
startActivity(intent); //開啟Activity2
②隐式意圖
通過指定action和category等屬性資訊,尋找Activity:
Intent intent = new Intent();
//設定action動作,該動作要跟清單檔案中設定的一樣
intent.setAction("edu.hzuapps.test.START_ACTIVITY");
startActivity(intent);
上述代碼隻指定了action,我們需要在Activity的清單檔案中添加如下代碼:
<intent-filter>
<!--設定action屬性值,需要在代碼中根據所設定的name打開指定的元件-->
<action android:name="edu.hzuapps.test.START_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
隻有當action和category屬性和目标元件設定的内容相同時,目标元件才會被開啟,每個Intent隻能指定一個action,但能指定多個category。
(2)實戰演練:打開浏覽器
建立一個名為OpenBrowser的應用程式
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/background"
tools:context=".MainActivity">
<Button
android:id="@+id/main_btn"
android:layout_width="147dp"
android:layout_height="55dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="144dp"
android:layout_marginRight="20dp"
android:background="@drawable/click" />
</RelativeLayout>
MainActivity.java:
package edu.hzuapps.openbrowser;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.main_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//設定動作為android.intent.action.VIEW
intent.setAction("android.intent.action.VIEW");
//設定要打開的網址
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
});
}
}
android.intent.action.VIEW是安卓系統内置的動作,通過該動作可以與浏覽器進行比對,還可以在這些元件之間傳遞資料。
5、Activity中的資料傳遞
Activity中的資料傳遞使用的也是Intent在元件之間傳遞資料。
(1)資料傳遞
在Activity中傳遞資料隻需要調用Intent的putExtra方法,通過該方法将要傳遞的資料暫存到Intent中,當另一個Activity啟動後,将這些資料中Intent中取出即可。
将Activity1的字元串傳遞到Activity2中:
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("extra_data","Hello Activity2");
startActivity(intent);
在Activity2中取出資料:
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
(2)實戰演練:注冊使用者資訊
注冊界面activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<ImageView
android:id="@+id/head"
android:layout_width="121dp"
android:layout_height="99dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:src="@drawable/head" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/head"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/register_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者名:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_name"
android:hint="請輸入使用者名"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/register_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView
android:id="@+id/tv_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="密 碼:"
android:textSize="20sp" />
<EditText
android:id="@+id/et_psw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_psw"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="@+id/btn_send"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_below="@+id/layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/register" />
</RelativeLayout>
資料展示界面activity_show.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="@+id/imav"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:background="@drawable/head" />
<LinearLayout
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者名:"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="密 碼:"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
界面互動實作資料傳遞MainActivity.java:
package edu.hzuapps.userregist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private EditText et_name;
private EditText et_psw;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_psw = (EditText) findViewById(R.id.et_psw);
btn_send = (Button) findViewById(R.id.btn_send);
//點選注冊按鈕進行資料傳輸
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passDate();
}
});
}
//資料傳輸函數
public void passDate() {
//建立Intent對象啟動ShowActivity
Intent intent = new Intent(this, ShowActivity.class);
//将資料存入Intent對象
intent.putExtra("username",et_name.getText().toString().trim());
intent.putExtra("password",et_psw.getText().toString().trim());
startActivity(intent);
}
}
ShowActivity.java:
package edu.hzuapps.userregist;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ShowActivity extends AppCompatActivity {
private TextView tv_name;
private TextView tv_password;
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_show);
//擷取Intent對象
Intent intent = getIntent();
//取出key對應的value值
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
tv_name = (TextView) findViewById(R.id.tv_name);
tv_password = (TextView) findViewById(R.id.tv_psw);
tv_name.setText("使用者名:"+username);
tv_password.setText("密 碼:"+password);
}
}
在注冊頁面中輸入使用者名密碼,點選注冊,此時會跳轉到資料展示界面:
(3)資料回傳
在Activity中,使用Intent可以将資料傳給下一個Activity,還可以将資料傳給上個Activity,使用startActivityForResult方法用于啟動Activity,可以在目前Activity銷毀時傳回結果給上一個Activity,例如微信朋友圈,進入圖庫選擇好照片後,會傳回到發表狀态頁的資訊并帶回所選圖檔資訊。
在Activity1中開啟Activity2:
Intent intent = new Intent(this, Activity2.class);
startActivityForResult(intent,1);
在Activity2中添加傳回資料:
Intent intent = new Intent();
intent.putExtra("extra_data","Hello Activity1");
setResult(1,intent);
注意startActivityForResult和setResult中的值要對應。由于使用startActivityForResult方法啟動Activity2,在Activity2被銷毀後會回調Activity1的onActivityResult方法,是以在Activity1中藥重寫該方法來得到傳回資料:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == 1) {
String string = data.getStringExtra("extra_data");
}
}
}
(4)實戰演練:線上店鋪
店鋪首頁activity_show.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".ShowActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="@+id/imgv"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:background="@drawable/head" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者名:"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="密 碼:"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/dialog_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dp"
android:text="Q寵官方旗艦店"
android:textSize="20sp" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Q寵生命值:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_life_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Q寵攻擊力:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_attack_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Q寵靈活度:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/tv_speed_progress"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btn_buy"
android:layout_width="160dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@android:color/holo_orange_dark"
android:text="買它"
android:textColor="#ffffff"
android:textSize="18sp"
android:onClick="click"
android:textStyle="bold" />
</LinearLayout>
購買界面activity_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#307f7f7f"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@android:drawable/ic_menu_info_details" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textSize="24sp"
android:text="商品名稱" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_life"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生命值"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_attack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="攻擊力"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="靈活度"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
封裝資訊ItemInfo.java:
package edu.hzuapps.shopping;
import java.io.Serializable;
public class ItemInfo implements Serializable {
private String name;
private String username;
private String password;
private int life;
private int attack;
private int speed;
public ItemInfo(String name, String username, String password, int life, int attack, int speed) {
this.name = name;
this.username = username;
this.password = password;
this.life = life;
this.attack = attack;
this.speed = speed;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
Intent除了傳遞基本類型外還能傳遞Serializable和Parcelable資料,這裡的ItemInfo并不是基本類型,是以要實作Serializable接口。
展示資訊ShopActivity.java:
package edu.hzuapps.shopping;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ShopActivity extends AppCompatActivity implements View.OnClickListener{
private ItemInfo itemInfo;
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_shop);
itemInfo = new ItemInfo("小A", "Jadore", "123456", 100,80,80);
findViewById(R.id.rl).setOnClickListener(this);
TextView mNameTV = (TextView) findViewById(R.id.tv_name);
TextView mLifeTV = (TextView) findViewById(R.id.tv_life);
TextView mAttackTV = (TextView) findViewById(R.id.tv_attack);
TextView mSpeedTV = (TextView) findViewById(R.id.tv_speed);
mNameTV.setText(itemInfo.getName()+"");
mLifeTV.setText("生命值+"+itemInfo.getLife());
mAttackTV.setText("攻擊力+"+itemInfo.getAttack());
mSpeedTV.setText("靈活度+"+itemInfo.getSpeed());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl:
Intent intent = new Intent();
intent.putExtra("Qlucky",itemInfo);
intent.putExtra("username",itemInfo.getUsername());
intent.putExtra("username",itemInfo.getPassword());
setResult(1,intent);
finish();
break;
}
}
}
在上述代碼中,首先建立一個Intent對象,然後在Intent對象中存放具體的裝備屬性資料,最後調用setResult方法跳轉Activity,setResult接受兩個參數,finish用于關閉目前的Activity。
展示界面代碼ShowActivity.java:
package edu.hzuapps.shopping;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ShowActivity extends AppCompatActivity {
private ProgressBar mProgressBar1;
private ProgressBar mProgressBar2;
private ProgressBar mProgressBar3;
private TextView tv_name;
private TextView tv_password;
private TextView mLifeTV;
private TextView mAttackTV;
private TextView mSpeedTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
//擷取Intent對象
Intent intent = getIntent();
mLifeTV = (TextView) findViewById(R.id.tv_life_progress);
mAttackTV = (TextView) findViewById(R.id.tv_attack_progress);
mSpeedTV = (TextView) findViewById(R.id.tv_speed_progress);
//初始化進度條
initProgress();
initUser();
}
private void initProgress() {
mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
mProgressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
mProgressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
//設定最大值
mProgressBar1.setMax(1000);
mProgressBar2.setMax(1000);
mProgressBar3.setMax(1000);
}
private void initUser() {
tv_name = (TextView) findViewById(R.id.tv_name);
tv_password = (TextView) findViewById(R.id.tv_password);
}
//開啟新的Activity并擷取其傳回值
public void click(View view) {
Intent intent = new Intent(this, ShopActivity.class);
//傳回請求結果,請求碼為1
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data != null) {
if(requestCode == 1) {
if(resultCode == 1) {
ItemInfo info = (ItemInfo) data.getSerializableExtra("Qlucky");
//更新ProgressBar的值
updateProgress(info);
//更新使用者名
updateUser(info);
}
}
}
}
private void updateUser(ItemInfo info) {
tv_name.setText("使用者名:"+info.getUsername());
tv_password.setText("密 碼:"+info.getPassword());
}
private void updateProgress(ItemInfo info) {
int progress1 = mProgressBar1.getProgress();
int progress2 = mProgressBar2.getProgress();
int progress3 = mProgressBar3.getProgress();
mProgressBar1.setProgress(progress1+info.getLife());
mProgressBar2.setProgress(progress2+info.getAttack());
mProgressBar3.setProgress(progress3+info.getSpeed());
mLifeTV.setText(mProgressBar1.getProgress()+"");
mAttackTV.setText(mProgressBar2.getProgress()+"");
mSpeedTV.setText(mProgressBar3.getProgress()+"");
}
}