android实验二第三部分
事情处理
1. 点击事件 (OnClickListener)
方法:setOnClickListener(View.OnClickListener listener)
android:onClick(在 XML 中使用)
核心功能:处理按钮、文本、图片等控件的 单击事件。
使用场景:按钮提交、页面跳转、功能触发等。
2. 长按事件 (OnLongClickListener)
方法:setOnLongClickListener(View.OnLongClickListener listener)
核心功能:处理控件的 长按事件,通常用于显示菜单或触发特殊功能。
使用场景:删除列表项、弹出上下文菜单、触发隐藏功能等。
3. 触摸事件 (OnTouchListener)
方法:setOnTouchListener(View.OnTouchListener listener)
核心功能:监听控件的 触摸操作(按下、抬起、移动),提供更精细的交互控制。
使用场景:自定义手势、拖动控件、滑动事件等。
4. 手势事件 (GestureDetector)
方法:GestureDetector.SimpleOnGestureListener
onFling()、onDoubleTap()、onScroll() 等
核心功能:检测更复杂的 手势事件,如滑动、双击、长按等。
使用场景:滑动页面、双击放大、快速滑动触发刷新等。
5. 按键事件 (onKeyDown() 和 onKeyUp())
方法:onKeyDown(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
核心功能:监听 物理按键(返回键、音量键、菜单键等)的按下与抬起事件。
使用场景:拦截返回键、音量键调整、截获硬件按键操作等。
6. 上下文菜单 (onCreateContextMenu() 和 onContextItemSelected())
方法:onCreateContextMenu()
onContextItemSelected(MenuItem item)
核心功能:处理 长按弹出菜单 的创建和点击事件。
使用场景:长按列表项弹出菜单、删除或编辑项、分享等功能。
7. 复用 OnClickListener 处理多个控件
方法:setOnClickListener(this) + switch(v.getId())
核心功能:使用一个 OnClickListener 实现 多个按钮或控件的点击事件。
使用场景:多按钮点击处理、界面中不同控件触发不同功能。
8. 列表项点击事件 (OnItemClickListener)
方法:setOnItemClickListener(AdapterView.OnItemClickListener listener)
核心功能:监听 ListView、GridView 项目的 单击事件。
使用场景:列表项跳转、点击加载详情、触发操作等。
9. RecyclerView 项点击事件
方法:在 Adapter 的 onBindViewHolder() 中添加点击事件。
核心功能:监听 RecyclerView 的 列表项点击事件。
使用场景:列表项详情跳转、打开对话框、加载子页面等。
10. ScrollView 滚动事件
方法:setOnScrollChangeListener(View.OnScrollChangeListener listener)
核心功能:监听 ScrollView 的 滚动事件。
使用场景:触底加载、滚动到顶部、动态显示/隐藏控件等。
11. SeekBar 进度改变事件
方法:setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener)
核心功能:监听 SeekBar 的 进度变化事件。
使用场景:音量调节、视频进度控制、亮度调节等。
12. Switch/Toggle 事件
方法:setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)
核心功能:监听 Switch、ToggleButton 的 状态切换事件。
使用场景:开关控制、模式切换、功能启用/禁用等。
13. RadioGroup 单选事件
方法:setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)
核心功能:监听 RadioGroup 的 选中项变化事件。
使用场景:选择模式、切换选项、触发不同功能等。
14. TextView 文本变化事件
方法:addTextChangedListener(TextWatcher watcher)
核心功能:监听 EditText 输入框的 文本变化事件。
使用场景:实时搜索、输入校验、动态提示等。
15. Spinner 选择项事件
方法:setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)
核心功能:监听 Spinner 下拉菜单的 选项选择事件。
使用场景:选择筛选条件、切换数据、改变界面状态等。
16. CheckBox/CompoundButton 事件
方法:setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)
核心功能:监听 CheckBox 的 选中与取消事件。
使用场景:多选操作、启用/禁用功能、批量选择等。
以下示例为点击按钮跳转:点击activity_process_query.xml中的“继续报工”按钮,会通过后端onClick()方法跳转到activity_process_entry.xml
activity_process_query.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"
android:padding="16dp"
android:background="#f5f5f5">
<!-- 标题 -->
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一道工序内容"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="16dp"
android:gravity="center" />
<!-- 工序名称 -->
<TextView
android:id="@+id/tv_process_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="工序:射蜡"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="16dp" />
<!-- 模具数量 -->
<TextView
android:id="@+id/tv_mold_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="模具数量:25"
android:textSize="16sp"
android:layout_marginBottom="8dp" />
<!-- 任务数量 -->
<TextView
android:id="@+id/tv_task_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="任务数量:25"
android:textSize="16sp"
android:layout_marginBottom="8dp" />
<!-- 实际完成数量 -->
<TextView
android:id="@+id/tv_actual_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="实际完成数量:23"
android:textSize="16sp"
android:layout_marginBottom="8dp" />
<!-- 废品数量 -->
<TextView
android:id="@+id/tv_scrap_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="废品数量:2"
android:textSize="16sp"
android:layout_marginBottom="16dp" />
<!-- 按钮容器 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:gravity="center">
<!-- 继续报工按钮 -->
<Button
android:id="@+id/btn_continue_report"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="继续报工"
android:backgroundTint="#5CACEE"
android:textColor="@android:color/white"
android:layout_marginEnd="8dp"
android:textSize="16sp" />
<!-- 完成按钮 -->
<Button
android:id="@+id/btn_complete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="完成"
android:backgroundTint="#5CACEE"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
ProcessQueryActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class ProcessQueryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_process_query); // 设置布局文件
// 获取报工按钮
Button btnSubmitReport = findViewById(R.id.btn_continue_report);
// 设置报工按钮点击事件
btnSubmitReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击报工按钮后跳转到 ProcessEntryActivity
Intent intent = new Intent(ProcessQueryActivity.this, ProcessEntryActivity.class);
startActivity(intent); // 启动 ProcessEntryActivity
}
});
}
}
activity_process_entry.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 工序选择 -->
<Spinner
android:id="@+id/sp_process_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
<!-- 模具数量 -->
<EditText
android:id="@+id/et_mold_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="模具数量"
android:inputType="number" />
<!-- 任务数量 -->
<EditText
android:id="@+id/et_task_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="任务数量"
android:inputType="number" />
<!-- 实际完成数量 -->
<EditText
android:id="@+id/et_actual_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="实际完成数量"
android:inputType="number" />
<!-- 废品数量 -->
<EditText
android:id="@+id/et_scrap_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="废品数量"
android:inputType="number" />
<!-- 提交按钮 -->
<Button
android:id="@+id/btn_submit"
android:backgroundTint="#5CACEE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:layout_marginTop="16dp" />
</LinearLayout>
</ScrollView>
以下是效果图;