竹子 (as记录)

一、创建另一个Activity------SecondActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.duan.project_two">

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"/>
    </application>

</manifest>

二、设计UI界面

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.duan.project_two.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="首页"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#fff"
        android:background="#00aaaa"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/bg">
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去竹园"
            android:background="@mipmap/btn_peach"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="200dp"
            android:layout_gravity="center"
            />

        <ImageView
            android:id="@+id/panda"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@mipmap/panda" />
        <ImageView
            android:id="@+id/bamboo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/bamboo"
            android:layout_alignBottom="@id/panda"
            android:layout_marginLeft="170dp"/>

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/bamboo"
            android:layout_toEndOf="@+id/btn"
            android:layout_toRightOf="@+id/btn" />
    </RelativeLayout>

</LinearLayout>

 

<?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="wrap_content"
        android:text="竹园"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#fff"
        android:background="#00aaaa"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/tree_bg">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/tree"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="100dp"
        android:orientation="vertical">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                >
                <ImageView
                    android:id="@+id/bamboo_1"
                    android:layout_marginLeft="70dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/bamboo"/>
            </LinearLayout>
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            >
            <ImageView
                android:id="@+id/bamboo_2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/bamboo"/>
             <ImageView
                 android:id="@+id/bamboo_3"
                 android:layout_weight="1"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:src="@mipmap/bamboo"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            >
            <ImageView
                android:id="@+id/bamboo_4"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/bamboo"/>
            <ImageView
                android:id="@+id/bamboo_5"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/bamboo"/>
            <ImageView
                android:id="@+id/bamboo_6"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/bamboo"/>
        </LinearLayout>

    </LinearLayout>
        <Button
            android:id="@+id/btn_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出竹园"
            android:background="@mipmap/btn_peach"
            android:layout_marginTop="400dp"
            />
    </LinearLayout>

</LinearLayout>

 三、处理逻辑交互

  先获取相应控件

    private void initView() {

        btnGoBamboo = findViewById(id.btn);

        textView = findViewById(id.textview);

    }

对控件添加监听

private void initListener() {
        btnGoBamboo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hangoutView();
            }
        });
    }

    private void hangoutView() {

        Intent intent=new Intent(this,SecondActivity.class);
        startActivityForResult(intent,0); //请求码:0

    }

重写onActivityResult(),获取返回的数据

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0&&resultCode==1){
            int numbers = data.getIntExtra("numbers", 0);
            textView.setText("摘到"+numbers+"个");
        }
    }

 

给竹子图片加点击事件

bamboo1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View view) {
                nums++;
                bamboo1.setImageAlpha(0);
            }
        });

    bamboo1.setImageAlpha(0);  //设置图片透明度为透明

点击退出按钮

 btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hangout();
            }
        });
   private void hangout() {

        Intent intent=new Intent();
        intent.putExtra("numbers",nums);
        setResult(1,intent); //返回码:1
        finish(); //关闭Activity
    }

用setResult()方法返回数据

主要是学会:

startActivityForResult()跳转页面请求获取返回数据,

setResult()返回包装的数据,

onActivityResult()接受数据

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值