AS edit view 代码 简单实现

本文详细介绍了一个典型的安卓登录界面的XML布局代码,包括TextView、EditText、Button等组件的使用,并通过Java代码实现了按钮点击事件及EditText的文字变化监听,展示了基本的用户界面设计与交互逻辑。

一、layout代码

<?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"
android:padding="15dp">

<TextView
    android:id="@+id/db"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="112dp"
    android:layout_marginTop="25dp"
    android:background="@drawable/fj" />

<EditText
    android:id="@+id/ed_01"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@id/db"
    android:layout_marginTop="20dp"
    android:textColor="#ff0000"
    android:textSize="16sp"
    android:hint="用户名/账号"
    android:inputType="number"
    android:drawableLeft="@mipmap/username"
    android:background="@drawable/bg_btn03"
    />
<EditText
    android:id="@+id/ed_02"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="35dp"
    android:textColor="#ff0000"
    android:textSize="16sp"
    android:layout_below="@+id/ed_01"
    android:hint="密码"
    android:inputType="textPassword"
    android:drawableLeft="@mipmap/passwd"
    android:background="@drawable/bg_btn03"
    />
<Button
    android:id="@+id/btn_dl"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="点我登录"
    android:textSize="18sp"
    android:layout_marginTop="20dp"
    android:layout_below="@id/ed_02"
    android:background="@drawable/bg_btn05"
    />

</RelativeLayout>

二、java代码

package com.relativelayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditViewActivity extends AppCompatActivity {
private Button btn_login;
private EditText ed01;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_view);
    btn_login = findViewById(R.id.btn_dl);
    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        //弹窗提示
        public void onClick(View v) {
            Toast.makeText(EditViewActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
        }
    });
    ed01 = findViewById(R.id.ed_01);
    ed01.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        //内容更改正当时捕捉。此处添加后可能会报错,根据报错信息修改即可
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String content = s.toString();
            Log.d("editViewUser",content);
            Log.d("length:",String.valueOf(content.length()));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

}

附图:
在这里插入图片描述

本文不再做单独说明,大多属性前面博客已有注释,部分属性望文生义,极少部分属性可自行百度。

实现“许愿墙”项目的核心功能,你需要按照以下步骤使用PHP编写代码: ### 1. 项目准备 #### 1.1 目录结构划分 项目的基本目录结构如下: ``` project/ ├── common/ │ ├── function.php │ └── init.php ├── css/ │ └── style.css ├── js/ │ └── common.js ├── view/ │ ├── common/ │ │ ├── add.html │ │ ├── edit.html │ │ └── password.html │ └── index.html ├── index.php └── save.php ``` #### 1.2 数据库设计 创建名为 `php_wish` 的数据库,并在其中创建 `wish` 表: ```sql CREATE DATABASE php_wish; USE php_wish; CREATE TABLE wish ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, name VARCHAR(12) DEFAULT '' NOT NULL, content VARCHAR(80) DEFAULT '' NOT NULL, time INT UNSIGNED DEFAULT 0 NOT NULL, color VARCHAR(10) DEFAULT '' NOT NULL, password VARCHAR(6) DEFAULT '' NOT NULL ); ``` #### 1.3 连接数据库 在 `common/init.php` 中编写连接数据库的代码: ```php <?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); // 数据库配置 $host = 'localhost'; $username = 'root'; $password = ''; $database = 'php_wish'; // 创建连接 $conn = new mysqli($host, $username, $password, $database); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?> ``` #### 1.4 准备公共函数 在 `common/function.php` 中编写公共函数: ```php <?php /** * 接收外部变量并进行过滤 * * @param string $name 变量名称 * @param mixed $default 默认值 * @param string $method 方法(GET/POST) * @param string $type 类型(s:字符串, d:整型, a:数组) * @return mixed */ function input($name, $default = '', $method = 'GET', $type = 's') { switch (strtoupper($method)) { case 'GET': $var = isset($_GET[$name]) ? $_GET[$name] : $default; break; case 'POST': $var = isset($_POST[$name]) ? $_POST[$name] : $default; break; default: return $default; } switch ($type) { case 's': // 字符串 return filter_var($var, FILTER_SANITIZE_STRING); case 'd': // 整型 return filter_var($var, FILTER_VALIDATE_INT); case 'a': // 数组 if (!is_array($var)) { return []; } return array_map('filter_var', $var, array_fill(0, count($var), FILTER_SANITIZE_STRING)); default: return $default; } } /** * 格式化日期 * * @param int $timestamp 时间戳 * @return string */ function format_date($timestamp) { $time = time(); $diff = $time - $timestamp; if ($diff < 60) { // 小于1分钟 return $diff . '秒前'; } elseif ($diff < 3600) { // 小于1小时 return floor($diff / 60) . '分钟前'; } elseif ($diff < 86400) { // 小于1天 return floor($diff / 3600) . '小时前'; } else { // 大于等于1天 return date('Y-m-d H:i:s', $timestamp); } } ?> ``` #### 1.5 引入公共文件 在 `index.php` 和 `save.php` 中引入公共文件: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; ?> ``` ### 2. 许愿墙展示 #### 2.1 查询所有愿望 在 `index.php` 中查询所有愿望: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; // 查询所有愿望 $sql = "SELECT id, name, content, time, color FROM wish"; $result = $conn->query($sql); $wishes = []; while ($row = $result->fetch_assoc()) { $row['time'] = format_date($row['time']); $wishes[] = $row; } // 关闭数据库连接 $conn->close(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>许愿墙</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div id="container"> <?php foreach ($wishes as $wish): ?> <div class="wish <?php echo htmlspecialchars($wish['color']); ?>"> <span><?php echo htmlspecialchars($wish['name']); ?></span> <p><?php echo htmlspecialchars($wish['content']); ?></p> <small><?php echo $wish['time']; ?></small> <a href="index.php?action=edit&id=<?php echo $wish['id']; ?>">修改</a> <a href="index.php?action=delete&id=<?php echo $wish['id']; ?>">删除</a> </div> <?php endforeach; ?> </div> <button onclick="showAddForm()">我要许愿</button> <!-- 添加愿望表单 --> <div id="add-form" style="display:none;"> <form action="save.php" method="post"> <label for="name">姓名:</label> <input type="text" name="name" required><br> <label for="content">愿望:</label> <textarea name="content" required></textarea><br> <label for="color">颜色:</label> <select name="color"> <option value="red">红色</option> <option value="blue">蓝色</option> <option value="green">绿色</option> </select><br> <label for="password">密码:</label> <input type="password" name="password"><br> <button type="submit">提交</button> </form> </div> <script src="js/common.js"></script> </body> </html> ``` ### 3. 发表愿望 #### 3.1 创建发表愿望表单 在 `view/common/add.html` 中创建表单: ```html <form action="save.php" method="post"> <label for="name">姓名:</label> <input type="text" name="name" required><br> <label for="content">愿望:</label> <textarea name="content" required></textarea><br> <label for="color">颜色:</label> <select name="color"> <option value="red">红色</option> <option value="blue">蓝色</option> <option value="green">绿色</option> </select><br> <label for="password">密码:</label> <input type="password" name="password"><br> <button type="submit">提交</button> </form> ``` #### 3.2 接收表单并进行过滤 在 `save.php` 中接收表单并进行过滤: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; $name = input('name', '', 'POST', 's'); $content = input('content', '', 'POST', 's'); $color = input('color', 'red', 'POST', 's'); $password = input('password', '', 'POST', 's'); // 验证输入长度 $name = mb_strimwidth($name, 0, 12, ''); $content = mb_strimwidth($content, 0, 80, ''); // 插入数据 $stmt = $conn->prepare("INSERT INTO wish (name, content, time, color, password) VALUES (?, ?, ?, ?, ?)"); $time = time(); $stmt->bind_param("ssiss", $name, $content, $time, $color, $password); if ($stmt->execute()) { header("Location: index.php"); exit; } else { echo "Error: " . $stmt->error; } $stmt->close(); $conn->close(); ?> ``` ### 4. 修改愿望 #### 4.1 验证保护密码 在 `index.php` 中验证保护密码: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; $action = input('action', '', 'GET', 's'); $id = input('id', 0, 'GET', 'd'); if ($action == 'edit' && $id > 0) { $sql = "SELECT * FROM wish WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $wish = $result->fetch_assoc(); $stmt->close(); if (isset($_POST['password'])) { $password = input('password', '', 'POST', 's'); if ($password == $wish['password']) { // 密码验证成功,显示修改表单 include 'view/common/edit.html'; exit; } else { echo "密码错误,请重新输入。"; } } else { // 显示输入密码的表单 include 'view/common/password.html'; exit; } } else { echo "愿望不存在。"; } } ?> ``` #### 4.2 接收修改愿望的表单 在 `save.php` 中接收修改愿望的表单: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; $id = input('id', 0, 'POST', 'd'); $name = input('name', '', 'POST', 's'); $content = input('content', '', 'POST', 's'); $color = input('color', 'red', 'POST', 's'); $password = input('password', '', 'POST', 's'); if ($id > 0) { // 验证输入长度 $name = mb_strimwidth($name, 0, 12, ''); $content = mb_strimwidth($content, 0, 80, ''); // 更新数据 $stmt = $conn->prepare("UPDATE wish SET name = ?, content = ?, color = ?, password = ? WHERE id = ?"); $stmt->bind_param("sssii", $name, $content, $color, $password, $id); if ($stmt->execute()) { header("Location: index.php"); exit; } else { echo "Error: " . $stmt->error; } } else { // 执行添加操作 $stmt = $conn->prepare("INSERT INTO wish (name, content, time, color, password) VALUES (?, ?, ?, ?, ?)"); $time = time(); $stmt->bind_param("ssiss", $name, $content, $time, $color, $password); if ($stmt->execute()) { header("Location: index.php"); exit; } else { echo "Error: " . $stmt->error; } } $stmt->close(); $conn->close(); ?> ``` ### 5. 删除愿望 在 `index.php` 中实现删除愿望的功能: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; $action = input('action', '', 'GET', 's'); $id = input('id', 0, 'GET', 'd'); if ($action == 'delete' && $id > 0) { $sql = "SELECT * FROM wish WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $wish = $result->fetch_assoc(); $stmt->close(); if (isset($_POST['password'])) { $password = input('password', '', 'POST', 's'); if ($password == $wish['password']) { // 密码验证成功,删除愿望 $stmt = $conn->prepare("DELETE FROM wish WHERE id = ?"); $stmt->bind_param("i", $id); if ($stmt->execute()) { header("Location: index.php"); exit; } else { echo "Error: " . $stmt->error; } } else { echo "密码错误,请重新输入。"; } } else { // 显示输入密码的表单 include 'view/common/password.html'; exit; } } else { echo "愿望不存在。"; } } ?> ``` ### 6. 分页查询 在 `index.php` 中实现分页查询: ```php <?php require_once 'common/init.php'; require_once 'common/function.php'; $page = input('page', 1, 'GET', 'd'); $size = 10; // 获取总记录数 $sql = "SELECT COUNT(*) AS total FROM wish"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $total = $row['total']; $total_pages = ceil($total / $size); // 分页查询 $start = ($page - 1) * $size; $sql = "SELECT id, name, content, time, color FROM wish ORDER BY id DESC LIMIT ?, ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("ii", $start, $size); $stmt->execute(); $result = $stmt->get_result(); $wishes = []; while ($row = $result->fetch_assoc()) { $row['time'] = format_date($row['time']); $wishes[] = $row; } // 关闭数据库连接 $stmt->close(); $conn->close(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>许愿墙</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div id="container"> <?php foreach ($wishes as $wish): ?> <div class="wish <?php echo htmlspecialchars($wish['color']); ?>"> <span><?php echo htmlspecialchars($wish['name']); ?></span> <p><?php echo htmlspecialchars($wish['content']); ?></p> <small><?php echo $wish['time']; ?></small> <a href="index.php?action=edit&id=<?php echo $wish['id']; ?>">修改</a> <a href="index.php?action=delete&id=<?php echo $wish['id']; ?>">删除</a> </div> <?php endforeach; ?> </div> <nav> <a href="index.php?page=1">首页</a> <a href="index.php?page=<?php echo max(1, $page - 1); ?>">上一页</a> <a href="index.php?page=<?php echo min($total_pages, $page + 1); ?>">下一页</a> <a href="index.php?page=<?php echo $total_pages; ?>">尾页</a> </nav> <button onclick="showAddForm()">我要许愿</button> <!-- 添加愿望表单 --> <div id="add-form" style="display:none;"> <form action="save.php" method="post"> <label for="name">姓名:</label> <input type="text" name="name" required><br> <label for="content">愿望:</label> <textarea name="content" required></textarea><br> <label for="color">颜色:</label> <select name="color"> <option value="red">红色</option> <option value="blue">蓝色</option> <option value="green">绿色</option> </select><br> <label for="password">密码:</label> <input type="password" name="password"><br> <button type="submit">提交</button> </form> </div> <script src="js/common.js"></script> </body> </html> ``` ### 7. 其他文件 - **CSS文件**:`css/style.css` - **JavaScript文件**:`js/common.js` - **HTML模板文件**:`view/common/add.html`, `view/common/edit.html`, `view/common/password.html`, `view/index.html` 这些文件的具体内容可以根据项目需求进行调整和完善。希望这些代码能帮助你顺利完成“许愿墙”项目!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值