Android开发-单选框/多选框

来源:互联网 发布:免费聊天软件 编辑:程序博客网 时间:2024/06/04 18:02

这是某位大哥做的单选框和多选框     可以参考 http://blog.csdn.net/ailiandeziwei/article/details/9164219


——————单选框(RadioButton)————

package com.example.radiobutton;

import Android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/*
 * 要完成单选框显示,我们需要使用到RadioGroup和RadioButton(单选框),RadioGroup用于对单选框进行分组,相同组内的单选框只有一个单选框能被选中。(例子代码请见下方备注栏)
 RadioGroup.check(R.id.dotNet);将id名为dotNet的单选框设置成选中状态。
(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。
RadioButton.getText();//获取单选框的值
调用setOnCheckedChangeListener()方法,处理单选框被选择事件,把RadioGroup.OnCheckedChangeListener实例作为参数传入
 */
public class MainActivity extends Activity {
private RadioGroup group_temo;
   private RadioButton checkRadioButton;
   
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group_temo = (RadioGroup)findViewById(R.id.radioGroup1);
//改变默认的选项
group_temo.check(R.id.radio1);
//获取默认被被选中值
checkRadioButton = (RadioButton) group_temo.findViewById(group_temo.getCheckedRadioButtonId());
//
//注册事件
group_temo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

//点击事件获取的选择对象
checkRadioButton = (RadioButton) group_temo.findViewById(checkedId);

Toast.makeText(getApplicationContext(), "您选择的专业是"+checkRadioButton.getText(), Toast.LENGTH_LONG).show();
}
});
}




}


——————多选框————————

package com.example.checkbutton;


import Java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
 /**
  * 每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。
 CheckBox.setChecked(true);//设置成选中状态。
 CheckBox.getText();//获取多选框的值
 调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入


  * @author John
  *
  */
public class MainActivity extends Activity {
// 声明多选列表对象
private CheckBox cbx1, cbx2, cbx3, cbx4;
private List<CheckBox> checkBoxs= new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbx1 = (CheckBox) findViewById(R.id.checkBox1);
cbx2 = (CheckBox) findViewById(R.id.checkBox2);
cbx3 = (CheckBox) findViewById(R.id.checkBox3);
cbx4 = (CheckBox) findViewById(R.id.checkBox4);

// 默认选项
cbx1.setChecked(true);
cbx3.setChecked(true);
// 注册事件
/*
* cbx1.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx2.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx3.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx4.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
*/
cbx1.setOnCheckedChangeListener(listener);
cbx2.setOnCheckedChangeListener(listener);
cbx3.setOnCheckedChangeListener(listener);
cbx4.setOnCheckedChangeListener(listener);


// 添加到集合中
checkBoxs.add(cbx1);
checkBoxs.add(cbx2);
checkBoxs.add(cbx3);
checkBoxs.add(cbx4);
}
  public void getValues(View v){
   String content = "";
   for(CheckBox cbx:checkBoxs){
    if(cbx.isChecked()){
    content+=cbx.getText()+"\n";
     
    }
   }
   if ("".equals(content)) {
content = "您还没有选择任何专业";
}
   new AlertDialog.Builder(this).setMessage(content).setTitle("您选择的专业有")
.setPositiveButton("关闭", null).show();
  } 
CompoundButton.OnCheckedChangeListener listener  = new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      CheckBox box = (CheckBox) buttonView;
      
      Toast.makeText(getApplicationContext(), "选择的专业是"+isChecked+box.getText(), Toast.LENGTH_LONG).show();

}
};

0 0
原创粉丝点击