Android移动应用开发——实验七——小鸭子报数(广播)

该文详细介绍了如何在Android应用中利用布局和控件创建一个界面,通过有序广播机制让小鸭子按优先级顺序报数。当点击大喇叭按钮时,小鸭子会按照设定的优先级依次显示数字。同时,展示了如何设置广播拦截,使得在特定情况下其他小鸭子不再报数。文章提供了具体的代码示例,包括XML布局文件和Java活动类的实现。
摘要由CSDN通过智能技术生成

    掌握布局与控件的使用方法
    掌握有序广播机制,根据广播接收者的优先级顺序接收广播
    掌握广播拦截机制

通过合理布局来搭建界面,界面效果如下图所示。采用有序广播方式,将下方小鸭子优先级分别设置1000、800、600。
1、当点击大喇叭后弹出“有序报数”,下方小鸭子从左到右依次报数“1”、“2”、“3”;
2、将第二个小鸭子优先级设置为1000,注册位置调整到左边第一个小鸭子前边,当点击大喇叭后弹出“有序报数”,下方中间小鸭子首先报数“1”,之后左边小鸭子报数“2”,最后右边小鸭子报数“3”;
3、设计拦截机制,当点击大喇叭后弹出“有序报数”,下方中间小鸭子首先报数“1”,之后其余小鸭子不再报数。
鼓励使用有序广播机制和广播拦截机制,自主设计类似功能的实验案例。


activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/count_ducks_bg"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="60dp">

        <ImageButton
            android:id="@+id/horn"
            android:layout_width="180dp"
            android:layout_height="150dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/horn" />

        <TextView
            android:id="@+id/content_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/horn"
            android:background="@drawable/content_left_bg"
            android:gravity="center"
            android:text="有序报数"
            android:textColor="@color/white"
            android:visibility="gone" />
    </RelativeLayout>

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="20dp"
            android:orientation="vertical">
            <TextView
                style="@style/tittle_style"
                android:id="@+id/duck_1"
                />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/duck" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:orientation="vertical">

            <TextView
                style="@style/tittle_style"
                android:id="@+id/duck_2"
                />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/duck" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:orientation="vertical">

            <TextView
                style="@style/tittle_style"
                android:id="@+id/duck_3"
                />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/duck" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

style.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="tittle_style">
        <item name="android:background">@drawable/tittle</item>
        <item name="android:layout_width">20dp</item>
        <item name="android:layout_height">20dp</item>
        <item name="android:visibility">gone</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_marginLeft">15dp</item>
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

这是设置鸭子报数的TextView的格式。

tittle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--    设置渐变颜色-->
    <gradient
        android:endColor="#fe451d"
        android:startColor="#fe957f"
        android:type="linear"/>
<!--    设置圆角-->
    <corners android:radius="180dp"/>
</shape>

这是style调用的背景格式。

MainActivity.java

package com.example.shiyan7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView duck1, duck2, duck3;
    ImageButton horn;
    TextView content_left;
    MyBroadCastReceiver1 myBroadCastReceiver1;
    MyBroadCastReceiver2 myBroadCastReceiver2;
    MyBroadCastReceiver3 myBroadCastReceiver3;
    private int num = 0;

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

    private void init() {
        duck1 = findViewById(R.id.duck_1);
        duck2 = findViewById(R.id.duck_2);
        duck3 = findViewById(R.id.duck_3);
        horn = findViewById(R.id.horn);
        content_left = findViewById(R.id.content_left);
        horn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                content_left.setVisibility(View.VISIBLE);
                horn.setClickable(false);
                Intent intent = new Intent();
                intent.setAction("Count_Duck");
                sendOrderedBroadcast(intent,null);
            }
        });
    }

    private void register_Receiver() {
        IntentFilter intentFilter1=new IntentFilter();
        intentFilter1.addAction("Count_Duck");
        intentFilter1.setPriority(1000);
//        intentFilter1.setPriority(800);
        myBroadCastReceiver1=new MyBroadCastReceiver1();
        registerReceiver(myBroadCastReceiver1,intentFilter1);
        IntentFilter intentFilter2=new IntentFilter();
        intentFilter2.addAction("Count_Duck");
        intentFilter2.setPriority(800);
//        intentFilter2.setPriority(1000);
        myBroadCastReceiver2=new MyBroadCastReceiver2();
        registerReceiver(myBroadCastReceiver2,intentFilter2);
        IntentFilter intentFilter3=new IntentFilter();
        intentFilter3.addAction("Count_Duck");
        intentFilter3.setPriority(600);
        myBroadCastReceiver3=new MyBroadCastReceiver3();
        registerReceiver(myBroadCastReceiver3,intentFilter3);


    }

    class MyBroadCastReceiver1 extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            num=num+1;
            duck1.setVisibility(View.VISIBLE);
            duck1.setText(num+"");
            Log.i("duck1","我收到了广播。");
            stop();
        }
    }

    class MyBroadCastReceiver2 extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            num=num+1;
            duck2.setVisibility(View.VISIBLE);
            duck2.setText(num+"");
            Log.i("duck2","我收到了广播。");
//            abortBroadcast();
//            Log.i("duck2","我拦截了广播。");
            stop();
        }
    }

    class MyBroadCastReceiver3 extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            num=num+1;
            duck3.setVisibility(View.VISIBLE);
            duck3.setText(String.valueOf(num));
            Log.i("duck3","我收到了广播。");
            stop();
        }
    }

    private void stop() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadCastReceiver1);
        unregisterReceiver(myBroadCastReceiver2);
        unregisterReceiver(myBroadCastReceiver3);
    }
}

以下是使用Android Studio实现鸭子的步骤: 1.创建一个新的Android Studio项目,选择空活动。 2.在activity_main.xml文件中添加一个ImageView控件和一个Button控件。 3.在res/drawable文件夹中添加一张鸭子的图片,命名为duck.png。 4.在res/values/styles.xml文件中添加一个名为duck_style的样式,用于设置ImageView控件的属性,代码如下: ```xml <style name="duck_style"> <item name="android:layout_width">120dp</item> <item name="android:layout_height">120dp</item> <item name="android:src">@drawable/duck</item> </style> ``` 5.在res/drawable文件夹中创建一个名为badge_bg.xml的文件,用于设置Button控件的背景,代码如下: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:endColor="#fe451d" android:startColor="#fe957f" android:type="linear" /> <corners android:radius="180dp" /> </shape> ``` 6.在MainActivity.java文件中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private int count = 0; private ImageView imageView; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); imageView.setImageResource(R.drawable.duck); imageView.setVisibility(View.INVISIBLE); button = findViewById(R.id.button); button.setBackgroundResource(R.drawable.badge_bg); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; if (count == 1) { imageView.setVisibility(View.VISIBLE); } Toast.makeText(MainActivity.this, "你了" + count + "只鸭子", Toast.LENGTH_SHORT).show(); } }); } } ``` 7.运行程序,点击按钮即可鸭子
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轨迹_6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值