Android开发之组件单选框(RadioButton)多选框
(CheckBox)下拉框(。。。
请⼤家伙多多指教:
邮箱:
请关注:ailiandeziwei
1.单选框(RadioButton)
ample.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;
android radiogroup/*
* 要完成单选框显⽰,我们需要使⽤到RadioGroup和RadioButton(单选框),RadioGroup⽤于对单选框进⾏分组,相同组内的单选框只有⼀个单选框能被选中。(例⼦代码请见下⽅备注栏)
RadioGroup.check(R.id.dotNet);将id名为dotNet的单选框设置成选中状态。
(RadioButton) CheckedRadioButtonId());//获取被选中的单选框。
调⽤setOnCheckedChangeListener()⽅法,处理单选框被选择事件,把RadioGroup.OnCheckedChangeListener实例作为参数传⼊
*/
public class MainActivity extends Activity {
private RadioGroup group_temo;
private RadioButton checkRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
group_temo = (RadioGroup) findViewById(R.id.radioGroup1);
//改变默认的选项
group_temo.check(R.id.radio1);
//获取默认被被选中值
checkRadioButton = (RadioButton) group_temo.findViewById(CheckedRadioButtonId());
//
//注册事件
group_temo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//点击事件获取的选择对象
checkRadioButton = (RadioButton) group_temo.findViewById(checkedId);
Toast.makeText(getApplicationContext(), "您选择的专业是"+Text(), Toast.LENGTH_LONG).show();
}
});
});
}
}
2.多选框;
ample.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);//设置成选中状态。
调⽤setOnCheckedChangeListener()⽅法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传⼊
* @author John
*
*/
public class MainActivity extends Activity {
// 声明多选列表对象
private CheckBox cbx1, cbx2, cbx3, cbx4;
private List<CheckBox> checkBoxs= new ArrayList<CheckBox>();
private List<CheckBox> checkBoxs= new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle 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+=Text()+"\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(), "选择的专业是"+Text(), Toast.LENGTH_LONG).show();
}
};
};
3.下拉框:
ample.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) { Create(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,View1});
//设置显⽰的数据
//spinner.setAdapter(arrayAdapter);
spinner.setAdapter(simpleAdapter);
Toast.makeText(getApplicationContext(),
"我相信"+SelectedItemPosition()), 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(), "冰⼼诀"+ItemAtPosition(position), Toast.LENGTH_LONG).show(); }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();
}
});
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论