请大家伙多多指教:
请关注:ailiandeziwei
1.单选框(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();
}
});
}
}
2.多选框;
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();
}
};
3.下拉框:
package com.example.spinner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
//声明spinner对象
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//根据ID获取对象
spinner = (Spinner) findViewById(R.id.spinner1);
//显示的数据
final String[] arr={
"爱你一万年",
"天使的翅膀",
"画地为老",
"下一个天亮",
"到不了",
"天下",
"开心马骝",
};
//adapter对象
//ArrayAdapter<String > arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("logo", R.drawable.mw);
map.put("text","最后一次的温柔");
Map<String,Object> map1 = new HashMap<String, Object>();
map1.put("logo", R.drawable.mw);
map1.put("text","Afterschool");
data.add(map);
data.add(map1);
SimpleAdapter simpleAdapter =
new SimpleAdapter(this, data, R.layout.activity_item, new String[]
{"logo","text"}, new int[]{R.id.imageView1,R.id.textView1});
//设置显示的数据
//spinner.setAdapter(arrayAdapter);
spinner.setAdapter(simpleAdapter);
Toast.makeText(getApplicationContext(),
"我相信"+spinner.getItemIdAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show();
//注册事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Spinner spinner = (Spinner) parent;
Toast.makeText(getApplicationContext(), "冰心诀"+spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();
}
});
}