带MySQL数据库的能够选择具体工作日的开关按钮Android闹钟设定代码

时间: 2023-06-17 20:03:38 浏览: 45
以下是一个基于Android的带有MySQL数据库的能够选择具体工作日的开关按钮的闹钟设定代码: 1. 在布局文件中添加以下代码: ```xml <Switch android:id="@+id/switchMonday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Monday" android:checked="true" /> <Switch android:id="@+id/switchTuesday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tuesday" android:checked="true" /> <Switch android:id="@+id/switchWednesday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Wednesday" android:checked="true" /> <Switch android:id="@+id/switchThursday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Thursday" android:checked="true" /> <Switch android:id="@+id/switchFriday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Friday" android:checked="true" /> <Switch android:id="@+id/switchSaturday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Saturday" android:checked="true" /> <Switch android:id="@+id/switchSunday" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sunday" android:checked="true" /> <Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> ``` 2. 在Java文件中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private Switch switchMonday, switchTuesday, switchWednesday, switchThursday, switchFriday, switchSaturday, switchSunday; private Button buttonSave; private String URL = "http://YOUR_DOMAIN_NAME/insert.php"; // Replace YOUR_DOMAIN_NAME with your actual domain name private RequestQueue requestQueue; private StringRequest request; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); switchMonday = findViewById(R.id.switchMonday); switchTuesday = findViewById(R.id.switchTuesday); switchWednesday = findViewById(R.id.switchWednesday); switchThursday = findViewById(R.id.switchThursday); switchFriday = findViewById(R.id.switchFriday); switchSaturday = findViewById(R.id.switchSaturday); switchSunday = findViewById(R.id.switchSunday); buttonSave = findViewById(R.id.buttonSave); requestQueue = Volley.newRequestQueue(this); buttonSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { saveData(); } }); } private void saveData() { String monday = switchMonday.isChecked() ? "1" : "0"; String tuesday = switchTuesday.isChecked() ? "1" : "0"; String wednesday = switchWednesday.isChecked() ? "1" : "0"; String thursday = switchThursday.isChecked() ? "1" : "0"; String friday = switchFriday.isChecked() ? "1" : "0"; String saturday = switchSaturday.isChecked() ? "1" : "0"; String sunday = switchSunday.isChecked() ? "1" : "0"; final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Saving data..."); progressDialog.setCancelable(false); progressDialog.show(); request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { @Override public void onResponse(String response) { progressDialog.dismiss(); Toast.makeText(getApplicationContext(), "Data saved successfully", Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { progressDialog.dismiss(); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("monday", monday); params.put("tuesday", tuesday); params.put("wednesday", wednesday); params.put("thursday", thursday); params.put("friday", friday); params.put("saturday", saturday); params.put("sunday", sunday); return params; } }; requestQueue.add(request); } } ``` 3. 创建一个名为"insert.php"的PHP文件,并将以下代码复制粘贴到该文件中: ```php <?php // Replace YOUR_MYSQL_HOST, YOUR_MYSQL_USERNAME, YOUR_MYSQL_PASSWORD, and YOUR_MYSQL_DATABASE_NAME with your actual MySQL database credentials $con = mysqli_connect("YOUR_MYSQL_HOST", "YOUR_MYSQL_USERNAME", "YOUR_MYSQL_PASSWORD", "YOUR_MYSQL_DATABASE_NAME") or die("Connection failed"); $monday = $_POST['monday']; $tuesday = $_POST['tuesday']; $wednesday = $_POST['wednesday']; $thursday = $_POST['thursday']; $friday = $_POST['friday']; $saturday = $_POST['saturday']; $sunday = $_POST['sunday']; $query = "INSERT INTO alarms (monday, tuesday, wednesday, thursday, friday, saturday, sunday) VALUES ('$monday', '$tuesday', '$wednesday', '$thursday', '$friday', '$saturday', '$sunday')"; if(mysqli_query($con, $query)) { echo "Data saved successfully"; } else { echo "Error saving data"; } mysqli_close($con); ?> ``` 这样,当用户点击“保存”按钮时,将向MySQL数据库插入所选工作日的值,并显示相应的消息。

相关推荐

最新推荐

recommend-type

Android连接MySQL数据库并进行增删改查操作示例讲解

主要介绍了Android 连接MySQL数据库并进行增删改查操作示例讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

使用phpexcel类实现excel导入mysql数据库功能(实例代码)

下面小编就为大家带来一篇使用phpexcel类实现excel导入mysql数据库功能(实例代码)。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

国家开放大学 MySQL数据库应用 实验训练1 在MySQL中创建数据库和表

国家开放大学 MySQL数据库应用 实验训练1 在MySQL中创建数据库和表
recommend-type

mysql数据库实验报告 数据表的操作

MySQL数据库的创建、查看、删除、使用命令。 表结构创建和修改、表约束的创建和修改; 表数据的插入、删除和修改; 表联系的创建和修改。
recommend-type

麒麟V10安装MySQL数据库

在虚拟机上安装一个国产系统麒麟V10,在麒麟V10上安装MySQL数据库
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。