Checkbox

Struct Checkbox 

Source
pub struct Checkbox { /* private fields */ }
Expand description

A Checkbox can be checked or unchecked

Implementations§

Source§

impl Checkbox

Source

pub fn new( activity: &mut Activity, text: &str, parent: Option<i64>, ) -> Result<Self>

Create a new Checkbox with optional initial checked state

Source

pub fn new_with_checked( activity: &mut Activity, text: &str, parent: Option<i64>, checked: bool, ) -> Result<Self>

Create a new Checkbox with specified checked state

Source

pub fn id(&self) -> i64

Get the view ID

Examples found in repository?
examples/checkbox_demo_v2.rs (line 115)
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}
Source

pub fn view(&self) -> &View

Get the underlying View

Examples found in repository?
examples/checkbox_demo_v2.rs (line 51)
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}
Source

pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()>

Set the checkbox text

Source

pub fn set_checked(&self, activity: &mut Activity, checked: bool) -> Result<()>

Set checked state

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V