界面控件 a View. OnClickListenero View On clicklistener0是ew定义的点击事件的监听器 接口,并在接口中仅定义了 onClick(函数 当 Button从 Android界面框架中接收到事件后,首先检查 这个事件是否是点击事件,如果是点击事件,同时 Button 又注册了监听器,则会调用该监听器中的onC|ckQ函数 每个View仅可以注册一个点击事件的监听器,如果使用 setonClicklistenero函数注册第二个点击事件的监听器, 之前注册的监听器将被自动注销 ■多个按钮注册到同一个点击事件的监听器上,代码如下
界面控件 ❑ View.OnClickListener() ◼ View.OnClickListener()是View定义的点击事件的监听器 接口,并在接口中仅定义了onClick()函数 ◼ 当Button从Android界面框架中接收到事件后,首先检查 这个事件是否是点击事件,如果是点击事件,同时Button 又注册了监听器,则会调用该监听器中的onClick()函数 ◼ 每个View仅可以注册一个点击事件的监听器,如果使用 setOnClickListener()函数注册第二个点击事件的监听器, 之前注册的监听器将被自动注销 ◼ 多个按钮注册到同一个点击事件的监听器上,代码如下
界面控件 1. Button On ClickListener buttonListener =new Button On ClicklistenerO oVerride public void onClick( View v)i switch(vgetId case r id Button 1 text View. setText(" Button按钮") return case R id Image Button01 text View. setText(" Image Button按钮"); 10. return 12 3. button. setOnClickListener(button Listener) 14. image Button set OnClickListener( button Listener); 第1行至第12行代码定义了一个名为 button listener的点击 事件监听器 第13行代码将该监听器注册到 Button上 第14行代码将该监听器注册到 mage Button上
界面控件 ◼ 第1行至第12行代码定义了一个名为buttonListener的点击 事件监听器 ◼ 第13行代码将该监听器注册到Button上 ◼ 第14行代码将该监听器注册到ImageButton上 1. Button.OnClickListener buttonListener = new Button.OnClickListener(){ 2. @Override 3. public void onClick(View v) { 4. switch(v.getId()){ 5. case R.id.Button01: 6. textView.setText("Button按钮"); 7. return; 8. case R.id.ImageButton01: 9. textView.setText("ImageButton按钮"); 10. return; 11. } 12. }}; 13. button.setOnClickListener(buttonListener); 14. imageButton.setOnClickListener(buttonListener);
界面控件 Button& Image Button控件布局 Activity函数 控件 o Button a ImageButton 口添加监听器及消息响应函数 a View: OnClickListener: On Click( 设置监听器:view: setOnClicklistener(
界面控件 ◼ Button & ImageButton控件布局 ◼ Activity函数 ◼ 控件 ❑ Button ❑ ImageButton ❑ 添加监听器及消息响应函数 ◼ View::OnClickListener::OnClick() ◼ 设置监听器:View::setOnClickListener()
界面控件 CheckBox和 RadioButton 口 Check Box:多选控件 口 RadioButton:单选控件 口 Radiogroup是 RadioButton的承载体,程序运行时不可 见,在每个 RadioGroup中,用户仅能够选择其中一个 RadioButton 6:02AM Checkbox Demo CheckBoxol. ischecked: true Check Box01 o CheckBox Radio Buttonon Radio Button02
界面控件 ◼ CheckBox和RadioButton ❑ CheckBox:多选控件 ❑ RadioButton:单选控件 ❑ RadioGroup是RadioButton的承载体,程序运行时不可 见,在每个RadioGroup中,用户仅能够选择其中一个 RadioButton
界面控件 a CheckboxRadiobutton demo在XML文件中的代码 <Text view android: id="@+id/ Text View01 android: layout width="fill parent 23456789 android: layout height="wrap content android: text="@string/hello"> <Check Box android: id=@+id/CheckBox0 1 android: layout width="wrap content android: layout height="wrap content android: text="CheckBox01"> </CheckBox> 10 <Check Box android: id="@+id/Check Box02 android: layout width="wrap content droid: layout height=w android text=CheckBox02"> 14 </CheckBox>
界面控件 ❑ CheckboxRadiobuttonDemo在XML文件中的代码 1. <TextView android:id="@+id/TextView01“ 2. android:layout_width="fill_parent" 3. android:layout_height="wrap_content" 4. android:text="@string/hello"/> 5. <CheckBox android:id="@+id/CheckBox01" 6. android:layout_width="wrap_content" 7. android:layout_height="wrap_content" 8. android:text="CheckBox01" > 9. </CheckBox> 10. <CheckBox android:id="@+id/CheckBox02" 11. android:layout_width="wrap_content" 12. android:layout_height="wrap_content" 13. android:text="CheckBox02" > 14. </CheckBox>