你好,游客 登录 注册 搜索
背景:
阅读新闻

Android游戏开发系统控件-CheckBox

[日期:2012-05-13] 来源:Linux社区  作者:wwj [字体:大 中 小]

=>>string.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="hello">CheckBoxProject!</string>  
  5.     <string name="app_name">CheckBox</string>  
  6.     <string name="cb1">CheckBox1</string>  
  7.     <string name="cb2">CheckBox2</string>  
  8.     <string name="cb3">CheckBox3</string>  
  9. </resources>  

=>>CheckBoxProject.java

  1. package com.checkBox;  
  2.   
  3. import Android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CheckBox;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.CompoundButton.OnCheckedChangeListener;  
  8. import android.widget.Toast;  
  9.   
  10.   
  11. public class CheckBoxActivity extends Activity implements OnCheckedChangeListener{  
  12.     private CheckBox cb1,cb2,cb3;//创建3个CheckBox对象   
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         //实例化3个CheckBox   
  19.         cb1 = (CheckBox) findViewById(R.id.cb1);  
  20.         cb2 = (CheckBox) findViewById(R.id.cb2);  
  21.         cb3 = (CheckBox) findViewById(R.id.cb3);  
  22.         cb1.setOnCheckedChangeListener(this);  
  23.         cb2.setOnCheckedChangeListener(this);  
  24.         cb3.setOnCheckedChangeListener(this);  
  25.     }  
  26.     //重写监听器的抽象函数   
  27.   
  28.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  29.         //buttonView 选中状态发生改变的那个按钮   
  30.         //isChecked 表示按钮新的状态(true/false)   
  31.         if(cb1 == buttonView || cb2 == buttonView || cb3 == buttonView ){  
  32.             if(isChecked){  
  33.                 //显示一个提示信息   
  34.                 toastDisplay(buttonView.getText() + "选中");  
  35.             }else{  
  36.                 toastDisplay(buttonView.getText() + "取消选中");  
  37.             }  
  38.         }     
  39.     }  
  40.     public void toastDisplay(String str){  
  41.         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  
  42.     }  
  43.       
  44. }  
linux
相关资讯       Andriod游戏开发  Andriod编程入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款