一、实验要求(本次使用图片放到最后,需要自拿)
通过合理布局来搭建界面,界面效果如下图所示。
采用有序广播方式,将下方小鸭子优先级分别设置1000、800、600。
1.当点击大喇叭后,弹出“有序报数”,下方小鸭子从左到右依次报数“1”、“2”、“3”;
(1000,800,600) (1,2,3)
2.将第二个小鸭子优先级设置为1000,注册位置调整到左边第一个小鸭子前边,当点击大喇叭后,弹出“有序报数”,下方中间小鸭子首先报数“1”,之后左边小鸭子报数“2”,最后右边小鸭子报数“3”;
(1000,1000,600)(2,1,3)
3.设计拦截机制,当点击大喇叭后,弹出“有序报数”,下方中间小鸭子首先报数“1”,之后其余小鸭子不再报数。
(1000,1000,600)(空,1,空)
二、实验结果展示
注:图片显示太快了,所以添加控制台语句判断谁先谁后,也可异步规定时间,但我不会
(1)初始界面
(2)点击按钮1 (然后点击上面对话框可消去数字)
(3)点击按钮2 (然后点击上面对话框可消去数字)
(4)加拦截代码(上面注:写的有)然后重开点按钮2
三、代码展示
(1)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"
tools:context=".MainActivity"
android:background="@drawable/river"
android:id="@+id/rtl">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"
android:text=" 数鸭子"
android:background="#008577"
android:textColor="@color/white"/>
<ImageView
android:id="@+id/iv_laba"
android:layout_width="179dp"
android:layout_height="125dp"
android:layout_alignTop="@+id/tv_title"
android:layout_marginTop="70dp"
android:background="@drawable/laba"
android:layout_marginLeft="220dp"/>
<ImageView
android:id="@+id/iv_baoshu"
android:visibility="invisible"
android:layout_width="144dp"
android:layout_height="125dp"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginLeft="100dp"
android:background="@drawable/baoshu"
/>
<!-- 以下是三个小黄鸭图片-->
<ImageView
android:id="@+id/x1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/xiaohuangya"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="350dp"
android:layout_marginLeft="20dp"/>
<ImageView
android:id="@+id/x2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/xiaohuangya"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="350dp"
android:layout_marginLeft="150dp"/>
<ImageView
android:id="@+id/x3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/xiaohuangya"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="350dp"
android:layout_marginLeft="280dp"/>
<!-- 以下是三个数字图片-->
<ImageView
android:visibility="invisible"
android:id="@+id/shu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shu_1"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="250dp"
android:layout_marginLeft="20dp"/>
<ImageView
android:visibility="invisible"
android:id="@+id/shu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shu_2"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="250dp"
android:layout_marginLeft="150dp"/>
<ImageView
android:visibility="invisible"
android:id="@+id/shu3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shu_3"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="250dp"
android:layout_marginLeft="280dp"/>
<!--以下是三个按钮 -->
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="要求1"
android:textSize="25sp"
android:backgroundTint="#1bbeff"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="550dp"
android:layout_marginLeft="20dp"/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="要求2"
android:textSize="25sp"
android:backgroundTint="#1bbeff"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="550dp"
android:layout_marginLeft="150dp"/>
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="要求3"
android:textSize="25sp"
android:backgroundTint="#1bbeff"
android:layout_alignTop="@+id/iv_laba"
android:layout_marginTop="550dp"
android:layout_marginLeft="280dp"/>
</RelativeLayout>
(2)java部分
package com.example.a7;
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.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.util.concurrent.Delayed;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public ImageView baoshu;
public Button yq_1,yq_2, yq_3;
public RelativeLayout relativeLayout;
public ImageView iv_1,iv_2,iv_3;
//动态添加的三个图片控件,也就是数字1,2,3
public ImageView shu1, shu2, shu3;
//三个广播接收者
s1Broadcast one;
s2Broadcast two;
s3Broadcast three;
s4Broadcast two2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
baoshu = findViewById(R.id.iv_baoshu);
relativeLayout = findViewById(R.id.rtl);
//小黄鸭的三张图片
iv_1 = findViewById(R.id.x1);
iv_2 = findViewById(R.id.x2);
iv_3 = findViewById(R.id.x3);
//三个数字图片
shu1 = findViewById(R.id.shu1);
shu2 = findViewById(R.id.shu2);
shu3 = findViewById(R.id.shu3);
//按钮的初始化
yq_1 = findViewById(R.id.btn_1);
yq_2 = findViewById(R.id.btn_2);
yq_3 = findViewById(R.id.btn_3);
yq_1.setOnClickListener(this);
yq_2.setOnClickListener(this);
yq_3.setOnClickListener(this);
baoshu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//按钮1
if (v.getId() == R.id.btn_1) {
registerReceiver1();
baoshu.setVisibility(View.VISIBLE);
Intent intent1 = new Intent();
intent1.setAction("111");
//发送广播
sendOrderedBroadcast(intent1, null);
}
//按钮2
if (v.getId() == R.id.btn_2) {
registerReceiver2();
baoshu.setVisibility(View.VISIBLE);
Intent intent2 = new Intent();
intent2.setAction("222");
//发送广播
sendOrderedBroadcast(intent2, null);
}
//按钮3,发送拦截广播
if (v.getId() == R.id.btn_3) {
registerReceiver3();
baoshu.setVisibility(View.VISIBLE);
Intent intent3 = new Intent();
intent3.setAction("333");
//发送广播
sendOrderedBroadcast(intent3, null);
}
//按让数字和报数图片消失
if (v.getId() == R.id.iv_baoshu) {
baoshu.setVisibility(View.INVISIBLE);
shu1.setVisibility(View.INVISIBLE);
shu2.setVisibility(View.INVISIBLE);
shu3.setVisibility(View.INVISIBLE);
}
}
public class s1Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("1111111111", "广播1");
shu1.setVisibility(View.VISIBLE);
}
}
public class s2Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("2222222222", "广播2");
shu2.setVisibility(View.VISIBLE);
}
}
public class s3Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("3333333333", "广播3");
shu3.setVisibility(View.VISIBLE);
}
}
public class s4Broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("拦截成功", "广播2");
shu2.setVisibility(View.VISIBLE);
abortBroadcast();
}
}
private void registerReceiver1() {
//动态注册广播1
one = new s1Broadcast();
IntentFilter filter1 = new IntentFilter();
filter1.setPriority(1000);
filter1.addAction("111");
registerReceiver(one, filter1);
//动态注册广播2
two = new s2Broadcast();
IntentFilter filter2 = new IntentFilter();
filter2.setPriority(800);
filter2.addAction("111");
registerReceiver(two, filter2);
//动态注册广播3
three = new s3Broadcast();
IntentFilter filter3 = new IntentFilter();
filter3.setPriority(600);
filter3.addAction("111");
registerReceiver(three, filter3);
}
//调换广播1,2顺序
private void registerReceiver2() {
//动态注册广播2
two = new s2Broadcast();
IntentFilter filter2 = new IntentFilter();
filter2.setPriority(1000);
filter2.addAction("222");
registerReceiver(two, filter2);
//动态注册广播1
one = new s1Broadcast();
IntentFilter filter1 = new IntentFilter();
filter1.setPriority(1000);
filter1.addAction("222");
registerReceiver(one, filter1);
//动态注册广播3
three = new s3Broadcast();
IntentFilter filter3 = new IntentFilter();
filter3.setPriority(600);
filter3.addAction("222");
registerReceiver(three, filter3);
}
//截止
private void registerReceiver3() {
//动态注册广播4
two2 = new s4Broadcast();
IntentFilter filter2 = new IntentFilter();
filter2.setPriority(1000);
filter2.addAction("333");
registerReceiver(two2, filter2);
//动态注册广播1
one = new s1Broadcast();
IntentFilter filter1 = new IntentFilter();
filter1.setPriority(1000);
filter1.addAction("333");
registerReceiver(one, filter1);
//动态注册广播3
three = new s3Broadcast();
IntentFilter filter3 = new IntentFilter();
filter3.setPriority(600);
filter3.addAction("333");
registerReceiver(three, filter3);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(one);
unregisterReceiver(two);
unregisterReceiver(three);
}
}
四、实验总结
本次实验主要收获就是知道了怎么用Java代码添加控件,同时对广播也有了一定理解、
但是这个方法问题很大,所以最后还是通过通过设置图片可见不可见来实现实验
五、实验素材