[转帖]Android软件开发之盘点常用系统控件界面大合集(二)_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2817 | 回复: 0   主题: [转帖]Android软件开发之盘点常用系统控件界面大合集(二)        下一篇 
wei.wang
注册用户
等级:少校
经验:1001
发帖:87
精华:0
注册:2013-8-29
状态:离线
发送短消息息给wei.wang 加好友    发送短消息息给wei.wang 发消息
发表于: IP:您无权察看 2013-9-9 10:04:34 | [全部帖] [楼主帖] 楼主

6.单项选择

       使用RadioGroup 包住若干个RadioButton 来实现单项选择。监听每一个RadioGroup 就可以知道那个单选组中的第一个ID被按下。

北京联动北方科技有限公司

  1. public class RadioActivity extends Activity {
  2.       
  3.        Context mContext = null;
  4.        @Override
  5.        protected void onCreate(Bundle savedInstanceState) {
  6.              setContentView(R.layout.radioview);
  7.              mContext = this;
  8.              //单选组(只有在一个组中的按钮可以单选)
  9.              RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radion0);
  10.              
  11.              //单选按钮(第一组)
  12.              final RadioButton radioButton0 = (RadioButton)findViewById(R.id.radionButton0);
  13.              final RadioButton radioButton1 = (RadioButton)findViewById(R.id.radionButton1);
  14.              final RadioButton radioButton2 = (RadioButton)findViewById(R.id.radionButton2);
  15.              
  16.              radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  17.                    
  18.                    @Override
  19.                    public void onCheckedChanged(RadioGroup arg0, int checkID) {
  20.                          if(radioButton0.getId() == checkID) {
  21.                                Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton0.getText(), Toast.LENGTH_LONG).show();
  22.                          }else if(radioButton1.getId() == checkID) {
  23.                                Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton1.getText(), Toast.LENGTH_LONG).show();
  24.                          }else if(radioButton2.getId() == checkID) {
  25.                                Toast.makeText(RadioActivity.this, "您选中了第一组" + radioButton2.getText(), Toast.LENGTH_LONG).show();
  26.                          }
  27.                    }
  28.              });
  29.              
  30.              RadioGroup radioGroup0 = (RadioGroup)findViewById(R.id.radion1);
  31.              
  32.              //单选按钮(第二组)
  33.              final RadioButton radioButton3 = (RadioButton)findViewById(R.id.radionButton3);
  34.              final RadioButton radioButton4 = (RadioButton)findViewById(R.id.radionButton4);
  35.              final RadioButton radioButton5 = (RadioButton)findViewById(R.id.radionButton5);
  36.              
  37.              radioGroup0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  38.                    
  39.                    @Override
  40.                    public void onCheckedChanged(RadioGroup arg0, int checkID) {
  41.                          if(radioButton3.getId() == checkID) {
  42.                                Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton3.getText(), Toast.LENGTH_LONG).show();
  43.                          }else if(radioButton4.getId() == checkID) {
  44.                                Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton4.getText(), Toast.LENGTH_LONG).show();
  45.                          }else if(radioButton5.getId() == checkID) {
  46.                                Toast.makeText(RadioActivity.this, "您选中了第二组" + radioButton5.getText(), Toast.LENGTH_LONG).show();
  47.                          }
  48.                    }
  49.              });
  50.              super.onCreate(savedInstanceState);
  51.        }
  52. }

复制代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <TextView android:layout_width="fill_parent"
  6.                           android:layout_height="wrap_content" 
  7.                           android:textColor="#000000"
  8.                           android:textSize="18dip"
  9.                           android:background="#00FF00"
  10.                       android:text="单项选择测试第一组" 
  11.                       android:gravity="center_vertical center_horizontal"
  12.                       />
  13.     <RadioGroup
  14.               android:id="@+id/radion0"
  15.                           android:layout_width="fill_parent"
  16.                           android:layout_height="wrap_content" >
  17.     <RadioButton
  18.                              android:id="@+id/radionButton0"
  19.                           android:layout_width="fill_parent"
  20.                           android:layout_height="wrap_content"
  21.                           android:text="item0" 
  22.     />
  23.      <RadioButton
  24.                              android:id="@+id/radionButton1"
  25.                           android:layout_width="fill_parent"
  26.                           android:layout_height="wrap_content"
  27.                           android:text="item1" 
  28.     />   
  29.      <RadioButton
  30.                              android:id="@+id/radionButton2"
  31.                           android:layout_width="fill_parent"
  32.                           android:layout_height="wrap_content"
  33.                           android:text="item2" 
  34.     />   
  35.     </RadioGroup>

  36.         <TextView android:layout_width="fill_parent"
  37.                           android:layout_height="wrap_content" 
  38.                           android:textColor="#000000"
  39.                           android:textSize="18dip"
  40.                           android:background="#00FF00"
  41.                       android:text="单项选择测试第二组" 
  42.                       android:gravity="center_vertical center_horizontal"
  43.                       />
  44.     <RadioGroup
  45.               android:id="@+id/radion1"
  46.                           android:layout_width="fill_parent"
  47.                           android:layout_height="wrap_content" >
  48.     <RadioButton
  49.                              android:id="@+id/radionButton3"
  50.                           android:layout_width="fill_parent"
  51.                           android:layout_height="wrap_content"
  52.                           android:text="item3" 
  53.     />
  54.      <RadioButton
  55.                              android:id="@+id/radionButton4"
  56.                           android:layout_width="fill_parent"
  57.                           android:layout_height="wrap_content"
  58.                           android:text="item4" 
  59.     />   
  60.      <RadioButton
  61.                              android:id="@+id/radionButton5"
  62.                           android:layout_width="fill_parent"
  63.                           android:layout_height="wrap_content"
  64.                           android:text="item5" 
  65.     />   
  66.     </RadioGroup>    
  67. </LinearLayout>

复制代码


7.多项选择

使用系统控件Checkbox  监听每一个checkbox 的点击事件就可以确定那几个选项被选择了。

北京联动北方科技有限公司

  1. public class CheckboxActivity extends Activity {
  2.       
  3.        //用来储存选中的内容
  4.        ArrayList <String>item = new ArrayList<String>();
  5.        
  6.        @Override
  7.        protected void onCreate(Bundle savedInstanceState) {
  8.              setContentView(R.layout.checkboxview);
  9.              
  10.              CheckBox checkbox0 = (CheckBox)findViewById(R.id.checkboxview0); 
  11.              CheckBox checkbox1 = (CheckBox)findViewById(R.id.checkboxview1); 
  12.              CheckBox checkbox2 = (CheckBox)findViewById(R.id.checkboxview2); 
  13.              CheckBox checkbox3 = (CheckBox)findViewById(R.id.checkboxview3); 
  14.              Button button = (Button)findViewById(R.id.checkboxbutton); 
  15.              //对checkbox进行监听
  16.              checkbox0.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  17.                   
  18.                    @Override
  19.                    public void onCheckedChanged(CompoundButton button, boolean arg1) {
  20.                          String str = button.getText().toString();
  21.                          if (button.isChecked()) {
  22.                                item.add(str);
  23.                          } else {
  24.                          item.remove(str);
  25.                    }
  26.                   
  27.              }
  28.        });
  29.        
  30.        checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  31.             
  32.              @Override
  33.              public void onCheckedChanged(CompoundButton button, boolean arg1) {
  34.                    String str = button.getText().toString();
  35.                    if (button.isChecked()) {
  36.                          item.add(str);
  37.                    } else {
  38.                    item.remove(str);
  39.              }
  40.             
  41.        }
  42.  }); 
  43.  checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  44.       
  45.        @Override
  46.        public void onCheckedChanged(CompoundButton button, boolean arg1) {
  47.              String str = button.getText().toString();
  48.              if (button.isChecked()) {
  49.                    item.add(str);
  50.              } else {
  51.              item.remove(str);
  52.        }
  53.       
  54.  }
  55.  });
  56.  checkbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  57.       
  58.        @Override
  59.        public void onCheckedChanged(CompoundButton button, boolean arg1) {
  60.              String str = button.getText().toString();
  61.              if (button.isChecked()) {
  62.                    item.add(str);
  63.              } else {
  64.              item.remove(str);
  65.        }
  66.       
  67.  }
  68.  });
  69.  
  70.  button.setOnClickListener(new OnClickListener() {
  71.        
  72.        @Override
  73.        public void onClick(View arg0) {
  74.              String str = item.toString();
  75.              Toast.makeText(CheckboxActivity.this, "您选中了" + str, Toast.LENGTH_LONG).show();
  76.              
  77.        }
  78.  });
  79.  super.onCreate(savedInstanceState);
  80.  }
  81. }

复制代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <TextView android:layout_width="fill_parent"
  6.                           android:layout_height="wrap_content" 
  7.                           android:textColor="#000000"
  8.                           android:textSize="18dip"
  9.                           android:background="#00FF00"
  10.                       android:text="多项选择测试" 
  11.                       android:gravity="center_vertical center_horizontal"
  12.                       />
  13.       <CheckBox
  14.                      android:id="@+id/checkboxview0"
  15.                   android:layout_width="fill_parent"
  16.                          android:layout_height="wrap_content" 
  17.                       android:text="item0"
  18.                       />   
  19.       <CheckBox
  20.                      android:id="@+id/checkboxview1"
  21.                   android:layout_width="fill_parent"
  22.                          android:layout_height="wrap_content" 
  23.                       android:text="item1"
  24.                       /> 
  25.       <CheckBox
  26.                      android:id="@+id/checkboxview2"
  27.                   android:layout_width="fill_parent"
  28.                          android:layout_height="wrap_content" 
  29.                       android:text="item2"
  30.                       /> 
  31.       <CheckBox
  32.                      android:id="@+id/checkboxview3"
  33.                   android:layout_width="fill_parent"
  34.                          android:layout_height="wrap_content" 
  35.                       android:text="item3"
  36.                       /> 
  37.       <Button
  38.                      android:id="@+id/checkboxbutton"
  39.                   android:layout_width="fill_parent"
  40.                          android:layout_height="wrap_content" 
  41.                       android:text="确定"
  42.                       /> 
  43. </LinearLayout>




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论