国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲

這篇具有很好參考價(jià)值的文章主要介紹了【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、功能介紹

該項(xiàng)目實(shí)現(xiàn)的功能主要有:

  1. 在首頁(yè)顯示一個(gè)按鈕點(diǎn)擊該按鈕跳轉(zhuǎn)到桃園頁(yè)面
  2. 在桃園頁(yè)面,點(diǎn)擊桃子會(huì)彈窗顯示摘到幾個(gè)桃子,同時(shí)被點(diǎn)擊桃子消失,總桃子數(shù)+1
  3. 點(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ù)。

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;
    }
}

三、效果展示

【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲,期末復(fù)習(xí),android,游戲,java
【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲,期末復(fù)習(xí),android,游戲,java
【Android開發(fā)】不同Activity之間的數(shù)據(jù)回傳實(shí)例(一)摘桃子游戲,期末復(fù)習(xí),android,游戲,java文章來(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android kotlin系列講解(入門篇)使用Intent在Activity之間穿梭

    返回總目錄 上一篇: Android kotlin系列講解(入門篇)Activity的理解與基本用法 ???????你應(yīng)該已經(jīng)對(duì)創(chuàng)建 Activity 的流程比較熟悉了,那我現(xiàn)在在 ActivityTest 項(xiàng)目再快速地創(chuàng)建一個(gè) Activity 。 ???????還是右擊 com.example.activitytest 包→ New → Activity → Empty Views Activity ,會(huì)彈出

    2024年02月12日
    瀏覽(25)
  • 【開發(fā)問(wèn)題】flink-cdc不用數(shù)據(jù)庫(kù)之間的,不同類型的轉(zhuǎn)化

    【開發(fā)問(wèn)題】flink-cdc不用數(shù)據(jù)庫(kù)之間的,不同類型的轉(zhuǎn)化

    我一開始是flink-cdc,oracle2Mysql,sql 我一開始直接用的oracle【date】類型,mysql【date】類型,sql的校驗(yàn)通過(guò)了,但是真正操作數(shù)據(jù)的時(shí)候報(bào)錯(cuò),告訴我oracle的數(shù)據(jù)格式的日期數(shù)據(jù),不可以直接插入到mysql格式的日期數(shù)據(jù),說(shuō)白了就是數(shù)據(jù)格式不一致導(dǎo)致的 我想的是既然格式不對(duì)

    2024年02月12日
    瀏覽(25)
  • Android開發(fā)筆記(三)—Activity篇

    Android開發(fā)筆記(三)—Activity篇

    (1)從當(dāng)前頁(yè)面跳到新頁(yè)面: startActivity(new Intent(源頁(yè)面.this,目標(biāo)頁(yè)面.class)) (2)從當(dāng)前頁(yè)面回到上一個(gè)頁(yè)面,相當(dāng)于關(guān)閉當(dāng)前頁(yè)面: finish() onCreate:創(chuàng)建活動(dòng)。把頁(yè)面布局加載到內(nèi)存,進(jìn)入了初始狀態(tài)。 onStart:開始活動(dòng)。把活動(dòng)頁(yè)面顯示在屏幕上,進(jìn)入了就緒狀態(tài)。 on

    2024年02月06日
    瀏覽(23)
  • Android開發(fā)-Activity中“android:exported“屬性的作用,以及“Permission Denial: starting Intent“錯(cuò)誤解決

    Android開發(fā)-Activity中“android:exported“屬性的作用,以及“Permission Denial: starting Intent“錯(cuò)誤解決

    如何在一個(gè)應(yīng)用程序中,啟動(dòng)另外一個(gè)應(yīng)用程序?最近正有這樣的需求,也踩了一個(gè)小坑。本節(jié)介紹使用Activity中\(zhòng)\\"android:exported\\\"屬性來(lái)實(shí)現(xiàn)這種訪問(wèn)。 Activity中\(zhòng)\\"android:exported\\\"屬性說(shuō)明: 在程序清單AndroidMenifest.xml文件中,可以設(shè)置這個(gè)屬性。 Android中的Activity中\(zhòng)\\"android:exported\\\"屬

    2024年02月06日
    瀏覽(18)
  • Qt/C++音視頻開發(fā)50-不同ffmpeg版本之間的差異處理

    ffmpeg的版本眾多,從2010年開始計(jì)算的項(xiàng)目的話,基本上還在使用的有ffmpeg2/3/4/5/6,最近幾年版本彪的比較厲害,直接4/5/6,大版本之間接口有一些變化,特別是一些廢棄接口被徹底刪除了,而網(wǎng)絡(luò)上的各種文章幾乎都是ffmpeg3左右為主的,所以本人在寫這個(gè)全功能播放組件的時(shí)

    2024年02月14日
    瀏覽(27)
  • Android開發(fā)頁(yè)面跳轉(zhuǎn)異常且應(yīng)用停止:android.content.ActivityNotFoundException: Unable to find explicit activity

    Android開發(fā)頁(yè)面跳轉(zhuǎn)異常且應(yīng)用停止:android.content.ActivityNotFoundException: Unable to find explicit activity

    E/AndroidRuntime: FATAL EXCEPTION: main ??? Process: com.example.app01, PID: 13901 ??? android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.app01/com.example.app01.RegisterActivity}; have you declared this activity in your AndroidManifest.xml? 根據(jù)日志信息不難發(fā)現(xiàn)問(wèn)題的所在,就是我們需要將存

    2024年02月16日
    瀏覽(23)
  • Mysql不同數(shù)據(jù)庫(kù)之間表結(jié)構(gòu)同步

    Mysql不同數(shù)據(jù)庫(kù)之間表結(jié)構(gòu)同步

    開發(fā)環(huán)境的Mysql表結(jié)構(gòu)做了修改,要同步到其他環(huán)境數(shù)據(jù)庫(kù)中使用數(shù)據(jù)庫(kù)管理工具JookDB的表結(jié)構(gòu)同步功能就很方便。雖然Navicat也有這個(gè)功能但是有免費(fèi)的當(dāng)然是用免費(fèi)的。 用JookDB添加數(shù)據(jù)庫(kù)后在數(shù)據(jù)庫(kù)節(jié)點(diǎn)上右鍵選擇“同步結(jié)構(gòu)”即可開始表結(jié)構(gòu)同步。 1.選擇結(jié)構(gòu)同步的源庫(kù)

    2024年02月05日
    瀏覽(24)
  • 不同activity項(xiàng)目創(chuàng)建時(shí)的區(qū)別

    不同activity項(xiàng)目創(chuàng)建時(shí)的區(qū)別

    在 Android Studio 中創(chuàng)建項(xiàng)目時(shí),可以選擇創(chuàng)建不同類型的 Activity 作為應(yīng)用程序的入口點(diǎn)。其中,包括 Empty Activity、Basic Activity、Empty Compose Activity 和 Basic Compose Activity 四種類型。 Empty Activity:這是最簡(jiǎn)單的 Activity 類型,它只包含一個(gè)空白的屏幕和一個(gè)默認(rèn)的菜單項(xiàng),可以在其中

    2024年01月21日
    瀏覽(20)
  • vue3+ts+element-plus實(shí)際開發(fā)之導(dǎo)出表格和不同類型之間相互賦值

    vue3+ts+element-plus實(shí)際開發(fā)之導(dǎo)出表格和不同類型之間相互賦值

    1. 安裝依賴 npm run xlsx 2. 引入,import * as XLSX from “xlsx”; 3. 報(bào)錯(cuò)找不到模塊“xlsx”或其相應(yīng)的類型聲明 修改成大寫就好了 import * as XLSX from \\\'XLSX\\\' ,如果沒有報(bào)提示就直接用 4. 使用導(dǎo)出文件 //---- 導(dǎo)出表 1. 直接用a標(biāo)簽下載 鼠標(biāo)移入樣式,點(diǎn)擊自動(dòng)下載 2. 有特殊數(shù)據(jù)需要解析

    2024年02月15日
    瀏覽(30)
  • SQL server 實(shí)現(xiàn)不同服務(wù)器之間的數(shù)據(jù)同步(作業(yè)實(shí)現(xiàn))

    SQL server 實(shí)現(xiàn)不同服務(wù)器之間的數(shù)據(jù)同步(作業(yè)實(shí)現(xiàn))

    ????????公司目前有兩臺(tái)服務(wù)器,一臺(tái)計(jì)劃用于存儲(chǔ)歷史數(shù)據(jù)(History),一臺(tái)計(jì)劃用于日常(Daily),現(xiàn)在的需求是將日常的SQL server產(chǎn)生的歷史數(shù)據(jù)表,通過(guò)定期同步的方式存儲(chǔ)到存放歷史數(shù)據(jù)的服務(wù)器上(Daily——History)這個(gè)順序一定要記清楚! 目錄 SQL server實(shí)現(xiàn)數(shù)據(jù)

    2024年02月16日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包