checkbox_demo_v2/
checkbox_demo_v2.rs

1// Checkbox 复选框演示 - 使用新库 API
2// 展示如何创建和使用 Checkbox,处理复选状态变化
3// 运行: cargo run --example checkbox_demo_v2 --release
4
5use termux_gui::{Activity, TextView, Result};
6use termux_gui::connection::read_message;
7
8// 更新状态显示的辅助函数
9fn update_status_text(
10    activity: &mut Activity,
11    status: &TextView,
12    wifi: bool,
13    bt: bool,
14    loc: bool,
15    notif: bool
16) -> Result<()> {
17    let mut selected = Vec::new();
18    if wifi { selected.push("WiFi"); }
19    if bt { selected.push("蓝牙"); }
20    if loc { selected.push("定位"); }
21    if notif { selected.push("通知"); }
22    
23    let text = if selected.is_empty() {
24        "当前选中: 无".to_string()
25    } else {
26        format!("当前选中: {}", selected.join(", "))
27    };
28    
29    status.set_text(activity, &text)?;
30    Ok(())
31}
32
33fn main() -> Result<()> {
34    println!("=== Checkbox 复选框演示 (新库版本) ===\n");
35    
36    // 创建 Activity(对话框模式)
37    let mut activity = Activity::new(true)?;
38    println!("✓ 连接建立\n");
39    
40    // 创建主布局
41    let layout = activity.create_linear_layout(None)?;
42    
43    // 创建标题
44    let title = activity.create_text_view("选择你喜欢的功能 ✅", Some(layout.id()))?;
45    title.set_text_size(&mut activity, 26)?;
46    title.view().set_margin(&mut activity, 10)?;
47    title.view().set_height_wrap_content(&mut activity)?;
48    
49    // 创建 Checkbox 1 - WiFi (初始未选中)
50    let checkbox1 = activity.create_checkbox_checked("📶 WiFi", Some(layout.id()), false)?;
51    checkbox1.view().set_margin(&mut activity, 5)?;
52    checkbox1.view().set_height_wrap_content(&mut activity)?;
53    
54    // 创建 Checkbox 2 - 蓝牙 (初始选中)
55    let checkbox2 = activity.create_checkbox_checked("📡 蓝牙", Some(layout.id()), true)?;
56    checkbox2.view().set_margin(&mut activity, 5)?;
57    checkbox2.view().set_height_wrap_content(&mut activity)?;
58    
59    // 创建 Checkbox 3 - 定位 (初始未选中)
60    let checkbox3 = activity.create_checkbox_checked("📍 定位服务", Some(layout.id()), false)?;
61    checkbox3.view().set_margin(&mut activity, 5)?;
62    checkbox3.view().set_height_wrap_content(&mut activity)?;
63    
64    // 创建 Checkbox 4 - 通知 (初始选中)
65    let checkbox4 = activity.create_checkbox_checked("🔔 通知", Some(layout.id()), true)?;
66    checkbox4.view().set_margin(&mut activity, 5)?;
67    checkbox4.view().set_height_wrap_content(&mut activity)?;
68    
69    // 创建分隔线
70    let separator = activity.create_text_view("━━━━━━━━━━━━━━━━━━━━", Some(layout.id()))?;
71    separator.view().set_height_wrap_content(&mut activity)?;
72    
73    // 创建状态显示
74    let status = activity.create_text_view("当前选中: 蓝牙, 通知", Some(layout.id()))?;
75    status.view().set_margin(&mut activity, 10)?;
76    status.view().set_height_wrap_content(&mut activity)?;
77    status.set_text_color(&mut activity, 0xFF2196F3u32 as i32)?;
78    
79    // 创建按钮
80    let apply_button = activity.create_button("✅ 应用设置", Some(layout.id()))?;
81    apply_button.view().set_margin(&mut activity, 10)?;
82    apply_button.view().set_height_wrap_content(&mut activity)?;
83    
84    println!("✓ 界面创建完成\n");
85    println!("━━━━━━━━━━━━━━━━━━━━━━");
86    println!("提示:");
87    println!("  • 点击复选框切换状态");
88    println!("  • 观察状态实时更新");
89    println!("  • 点击 '应用设置' 查看最终选择");
90    println!("━━━━━━━━━━━━━━━━━━━━━━\n");
91    
92    // 状态跟踪
93    let mut wifi_checked = false;
94    let mut bluetooth_checked = true;
95    let mut location_checked = false;
96    let mut notification_checked = true;
97    
98    // 事件循环
99    loop {
100        let event = read_message(activity.event_stream())?;
101        let event_type = event["type"].as_str().unwrap_or("");
102        let event_value = &event["value"];
103        
104        match event_type {
105            "destroy" => {
106                // Activity 已被系统销毁,直接退出即可
107                // 不要调用 activity.finish(),因为 Activity 已经不存在了
108                println!("\n✓ Activity 已关闭");
109                return Ok(());
110            },
111            "click" => {
112                let clicked_id = event_value["id"].as_i64().unwrap_or(-1);
113                let is_checked = event_value["set"].as_bool().unwrap_or(false);
114                
115                if clicked_id == checkbox1.id() {
116                    wifi_checked = is_checked;
117                    println!("📶 WiFi: {}", if is_checked { "开启" } else { "关闭" });
118                    update_status_text(&mut activity, &status, 
119                                     wifi_checked, bluetooth_checked, 
120                                     location_checked, notification_checked)?;
121                    
122                } else if clicked_id == checkbox2.id() {
123                    bluetooth_checked = is_checked;
124                    println!("📡 蓝牙: {}", if is_checked { "开启" } else { "关闭" });
125                    update_status_text(&mut activity, &status, 
126                                     wifi_checked, bluetooth_checked, 
127                                     location_checked, notification_checked)?;
128                    
129                } else if clicked_id == checkbox3.id() {
130                    location_checked = is_checked;
131                    println!("📍 定位: {}", if is_checked { "开启" } else { "关闭" });
132                    update_status_text(&mut activity, &status, 
133                                     wifi_checked, bluetooth_checked, 
134                                     location_checked, notification_checked)?;
135                    
136                } else if clicked_id == checkbox4.id() {
137                    notification_checked = is_checked;
138                    println!("🔔 通知: {}", if is_checked { "开启" } else { "关闭" });
139                    update_status_text(&mut activity, &status, 
140                                     wifi_checked, bluetooth_checked, 
141                                     location_checked, notification_checked)?;
142                    
143                } else if clicked_id == apply_button.id() {
144                    println!("\n✅ 应用设置:");
145                    println!("  WiFi: {}", if wifi_checked { "✓" } else { "✗" });
146                    println!("  蓝牙: {}", if bluetooth_checked { "✓" } else { "✗" });
147                    println!("  定位: {}", if location_checked { "✓" } else { "✗" });
148                    println!("  通知: {}", if notification_checked { "✓" } else { "✗" });
149                    
150                    // 显示确认消息
151                    status.set_text(&mut activity, "✅ 设置已应用!")?;
152                    status.set_text_color(&mut activity, 0xFF4CAF50u32 as i32)?;
153                }
154            },
155            _ => {}
156        }
157    }
158}