Android—幸运抽奖火箭发射倒计时(第六次作业)

2023-11-11 12:04

本文主要是介绍Android—幸运抽奖火箭发射倒计时(第六次作业),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android—幸运抽奖&&点火发射(第六次作业)

创建项目

image-20231110114708197

准备工作

修改按钮的颜色,如果不修改这行代码,那么后期给按钮添加background属性的时候,按钮并不会发生变化。

image-20231110114553928

设置按钮的样式文件btn_press_blue.xml,设置了按压效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true">     <!--按压--><shape><solid android:color="#0082FF"/><corners android:radius="10dp"/></shape></item><item android:state_pressed="false"><shape><solid android:color="@color/blue"/><corners android:radius="10dp"/></shape></item>
</selector>

其中蓝色的我选择的RGB是#FF7BBAF7

<color name="blue">#FF7BBAF7</color>

幸运抽奖

幸运⼤抽奖。⽤户单击“开始抽奖”Button后启动线程,开始抽奖过程,在界⾯上随机出现抽奖名单。用户点击”揭晓大奖“Button后,终止线程运行。

image-20231110142546147

左侧为项目结构,红色方框为新增内容或者修改的内容,右侧简单展示上述准备工作中的局部代码。

布局文件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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_start"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="开始抽奖了"android:textSize="20sp"android:background="@drawable/btn_press_blue"android:layout_margin="5dp"/><Buttonandroid:id="@+id/btn_stop"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="揭晓大奖"android:textSize="20sp"android:background="@drawable/btn_press_blue"android:layout_margin="5dp"/></LinearLayout><TextViewandroid:id="@+id/label"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:textSize="40sp"android:gravity="center"android:layout_gravity="center_horizontal"android:textColor="@color/blue"/><TextViewandroid:id="@+id/information"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="150dp"android:textSize="20sp"android:gravity="center"android:layout_gravity="center_horizontal"android:textColor="@color/blue"android:text="WeiComp 计科20xx 20200707xxxx 制作"/></LinearLayout>

image-20231110144647839

  1. 创建RandomService的java文件

    package com.example.servicedemo;public class RandomService extends Service {	//导包的时候导入android.app.Serviceprivate Thread luckThread;@Overridepublic void onCreate() {super.onCreate();Toast.makeText(this, "幸运大抽奖开始", Toast.LENGTH_SHORT).show();luckThread = new Thread(null, backgroundWork, "luckThread");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Toast.makeText(this, "抽奖进行中", Toast.LENGTH_SHORT).show();if (!luckThread.isAlive()) {luckThread.start();}}@Overridepublic void onDestroy() {super.onDestroy();Toast.makeText(this, "恭喜你中奖了", Toast.LENGTH_SHORT).show();luckThread.interrupt();}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}private Runnable backgroundWork = new Runnable() {@Overridepublic void run() {try {while (!Thread.interrupted()) {int randomDouble = (int) Math.round(Math.random() * 2 + 1);int randomDouble1 = (int) Math.round(Math.random() * 2);int randomDouble2 = (int) Math.round(Math.random() * 9);MainActivity.UpdateGUI(randomDouble, randomDouble1, randomDouble2);Thread.sleep(1000);if (randomDouble1 == randomDouble2 && randomDouble1 == 0) {luckThread.interrupt();}}} catch (InterruptedException e) {e.printStackTrace();}}};
    }
    
  2. MainAcitivty.java函数

    package com.example.servicedemo;public class MainActivity extends AppCompatActivity {private Button btn_start, btn_stop;private static Handler handler = new Handler();private static TextView labelView = null;private static int randomDouble, randomDouble1, randomDouble2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_start = findViewById(R.id.btn_start);btn_stop = findViewById(R.id.btn_stop);labelView = findViewById(R.id.label);handler = new Handler();final Intent serviceIntent = new Intent(this, RandomService.class);btn_start.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {startService(serviceIntent);}});btn_stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {stopService(serviceIntent);handler.post(RefreshLable);}});}@Overridepublic void onDestroy() {super.onDestroy();MainActivity.UpdateGUI(randomDouble, randomDouble1, randomDouble2);Toast.makeText(this, "恭喜你中奖了", Toast.LENGTH_SHORT).show();}public static void UpdateGUI(int refreshDouble, int refreshDouble1, int refreshDouble2) {randomDouble = refreshDouble;randomDouble1 = refreshDouble1;randomDouble2 = refreshDouble2;handler.post(RefreshLable);}private static Runnable RefreshLable = new Runnable() {@Overridepublic void run() {labelView.setText(String.valueOf("20200707" + randomDouble +randomDouble1 +randomDouble2));}};
    }
    
  3. 修改AndroidManifest.xml代码(确保将服务的启动和停止操作正确注册在 AndroidManifest.xml 中。)

    <application>...<serviceandroid:name=".RandomService"android:enabled="true"android:exported="false" />...
    </application>
    

    image-20231110145128774

效果图

image-20231110144517588

火箭发射倒计时

使用Timer实现火箭倒计时功能。(参照例6-6)请在界面适当的位置添加个人信息,如姓名或拼音,以防止作业雷同。上传代码及模拟器图片。

【例6-6】火箭点火倒计时。在UI界面上设计一个TextView和一个Button。当用户点击Button时,启动Timer工作,在TextView上显示10至1的倒计时数字,数字每秒变化一次。当显示到1后,TextView上显示“点火成功”。

布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="10dp"><TextViewandroid:id="@+id/textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:textSize="40sp"android:textColor="@color/blue"android:gravity="center" /><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/btn_press_blue"android:text="点火发射"android:textSize="25sp"android:textColor="@color/white"android:layout_centerInParent="true" /></RelativeLayout>

image-20231110151933448

MainActivity.java

package com.example.timerdemo;public class MainActivity extends AppCompatActivity {private Button button;private TextView textView;private Timer timer;private TimerTask timerTask;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textview);button = findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startTimer();}});}private void startTimer() {timer = new Timer();timerTask = new TimerTask() {int i = 10; //倒计时数目@Overridepublic void run() {Message message = Message.obtain();message.what = i;handler.sendMessage(message);i--;}};timer.schedule(timerTask, 1000, 1000);}@SuppressLint("HandlerLeak")private Handler handler = new Handler() {@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);if (msg.what > 0) {textView.setText(" " + msg.what);} else {// 在handler里可以更改UI组件textView.setText("点火成功");timer.cancel();}}};
}

运行结果

image-20231110151843564

这篇关于Android—幸运抽奖火箭发射倒计时(第六次作业)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/389937

相关文章

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio