一、功能介紹
該項(xiàng)目實(shí)現(xiàn)的功能主要有:
- 在首頁(yè)顯示一個(gè)按鈕點(diǎn)擊該按鈕跳轉(zhuǎn)到桃園頁(yè)面
- 在桃園頁(yè)面,點(diǎn)擊桃子會(huì)彈窗顯示摘到幾個(gè)桃子,同時(shí)被點(diǎn)擊桃子消失,總桃子數(shù)+1
- 點(diǎn)擊退出桃園會(huì)返回首頁(yè),首頁(yè)桃子數(shù)會(huì)根據(jù)點(diǎn)擊的桃子數(shù)動(dòng)態(tài)增加
二、代碼實(shí)現(xiàn)
1. 資源準(zhǔn)備
將項(xiàng)目所需要的圖片bg.png、monkey.png、btn_peach.png、peach_pic.png 導(dǎo)入程序的drawablehdpi文件夾中(默認(rèn)情況下程序中沒有drawable-hdpi 文件夾,需手動(dòng)在res 文件夾中創(chuàng)建一個(gè))。
2. 布局文件設(shè)計(jì)
2.1 主Activity
在layout文件夾中編輯activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="首頁(yè)"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_monkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/monkey" />
<Button
android:id="@+id/btn_peach"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="250dp"
android:layout_toRightOf="@id/iv_monkey"
android:background="@drawable/btn_peach"
android:gravity="center"
android:text="去桃園"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ImageView
android:id="@+id/iv_peach"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_below="@+id/btn_peach"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="@drawable/peach_pic" />
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_peach"
android:layout_marginTop="25dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_peach"
android:text="摘到0 個(gè)"
android:textColor="@android:color/black"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
2.2 輔Activity
在layout文件夾中新增activity_peach.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#008577"
android:gravity="center"
android:text="桃園"
android:textColor="@android:color/white"
android:textSize="20sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tree_bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="70dp"
android:background="@drawable/tree">
<Button
android:id="@+id/btn_one"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="35dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_two"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="80dp"
android:layout_marginTop="5dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_three"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_below="@id/btn_one"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/btn_two"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_four"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_five"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="70dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/btn_four"
android:background="@drawable/peach_pic" />
<Button
android:id="@+id/btn_six"
android:layout_width="55dp"
android:layout_height="40dp"
android:layout_below="@id/btn_two"
android:layout_marginLeft="45dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@id/btn_five"
android:background="@drawable/peach_pic" />
</RelativeLayout>
<Button
android:id="@+id/btn_exit"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="50dp"
android:background="@drawable/btn_peach"
android:text="退出桃園"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="18sp"/>
</RelativeLayout>
</LinearLayout>
2.3 其他資源文件
主題文件themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PickPeach" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="MyCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
3. 實(shí)現(xiàn)邏輯功能
3.0 全局配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hzj.pickpeach">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/MyCustomTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PeachActivity"
android:exported="true">
</activity>
</application>
</manifest>
3.1 首頁(yè)功能
在MainActivity 中實(shí)現(xiàn)“去桃園”按鈕的點(diǎn)擊事件,當(dāng)點(diǎn)擊按鈕時(shí)程序跳轉(zhuǎn)到桃園摘桃的界面。同時(shí),還需要接收桃園界面回傳過(guò)來(lái)的桃子個(gè)數(shù)。
public class MainActivity extends AppCompatActivity {
private Button btn_peach;
private TextView tv_count;
private int totalCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btn_peach = findViewById(R.id.btn_peach);
tv_count = findViewById(R.id.tv_count);
//為按鈕設(shè)置點(diǎn)擊事件監(jiān)聽器
btn_peach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//設(shè)置意圖對(duì)象,實(shí)現(xiàn)界面跳轉(zhuǎn)
Intent intent = new Intent(MainActivity.this, PeachActivity.class);
startActivityForResult(intent, 1);
// startActivity(intent);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判斷請(qǐng)求碼和返回碼是否正確
if (requestCode==1 && requestCode==1) {
//獲取回傳的數(shù)據(jù)
int count = data.getIntExtra("count", 0);;
totalCount = totalCount + count;
tv_count.setText("摘到" + totalCount + "個(gè)");
}
}
}
3.2 桃園界面的摘桃效果
點(diǎn)擊桃子實(shí)現(xiàn)摘桃效果,同時(shí)全局桃子數(shù)動(dòng)態(tài)變化,點(diǎn)擊退出桃園,返回首頁(yè),同時(shí)更新首頁(yè)桃子數(shù)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-816569.html
public class PeachActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_exit;
private int count = 0;//桃子個(gè)數(shù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_peach);
init();
}
private void init() {
btn_one = findViewById(R.id.btn_one);
btn_two = findViewById(R.id.btn_two);
btn_three = findViewById(R.id.btn_three);
btn_four = findViewById(R.id.btn_four);
btn_five = findViewById(R.id.btn_five);
btn_six = findViewById(R.id.btn_six);
btn_exit = findViewById(R.id.btn_exit);
btn_one.setOnClickListener(this);
btn_two.setOnClickListener(this);
btn_three.setOnClickListener(this);
btn_four.setOnClickListener(this);
btn_five.setOnClickListener(this);
btn_six.setOnClickListener(this);
btn_exit.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_one: //第一個(gè)桃子的點(diǎn)擊事件
info(btn_one);
break;
case R.id.btn_two: //第二個(gè)桃子的點(diǎn)擊事件
info(btn_two);
break;
case R.id.btn_three: //第三個(gè)桃子的點(diǎn)擊事件
info(btn_three);
break;
case R.id.btn_four: //第四個(gè)桃子的點(diǎn)擊事件
info(btn_four);
break;
case R.id.btn_five: //第五個(gè)桃子的點(diǎn)擊事件
info(btn_five);
break;
case R.id.btn_six: //第六個(gè)桃子的點(diǎn)擊事件
info(btn_six);
break;
case R.id.btn_exit: //“退出桃園”按鈕的點(diǎn)擊事件
returnData();
break;
default:
throw new IllegalStateException("Unexpected value: " + view.getId());
}
}
/**
* 按鈕的點(diǎn)擊事件處理
*/
private void info(Button btn) {
//桃子個(gè)數(shù)加1
count++;
//被摘掉的桃子設(shè)置為隱藏不可見
btn.setVisibility(View.INVISIBLE);
Toast.makeText(PeachActivity.this, "摘到" + count + "個(gè)桃子",
Toast.LENGTH_LONG).show();
}
/**
* 將數(shù)據(jù)回傳到上個(gè)界面
*/
private void returnData() {
//設(shè)置意圖對(duì)象,將摘桃子的個(gè)數(shù)回傳給首頁(yè)界面
Intent intent = new Intent();
intent.putExtra("count", count);
//返回碼標(biāo)識(shí)
setResult(1, intent);
//銷毀本界面
PeachActivity.this.finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//當(dāng)點(diǎn)擊設(shè)備返回鍵時(shí),調(diào)用數(shù)據(jù)回傳方法,返回首頁(yè)界面
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
returnData();
}
return false;
}
}
三、效果展示
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-816569.html
到了這里,關(guān)于【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!