1. 日期选择器 比较丑,可能用的很少
public class MainActivity extends ActionBarActivity { private Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) this.findViewById(R.id.btnChoose); btn.setOnClickListener(new View.OnClickListener() { // 定义了一个按钮,然后点显示日期组件 @Override public void onClick(View v) { new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { // 当日期选择的时候 String date = String.format("%d-%d-%d", year, month, dayOfMonth); btn.setText(date); // 改变按钮的text System.out.print(date); // 打印 } }, 2016, 1, 1).show(); } }); System.out.println("onCreate"); }}
2. 时间控件
public class MainActivity extends ActionBarActivity { private Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) this.findViewById(R.id.btnChoose); btn.setOnClickListener(new View.OnClickListener() { // 定义了一个按钮,然后点显示时间控件 @Override public void onClick(View v) { // 创建一个时间控件 new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // 选择时间市,相应 String s = String.format("%d:%d", hourOfDay, minute); btn.setText(s); } }, 0, 0, true).show(); } }); System.out.println("onCreate"); } }
3. 单选按钮
视图xml文件
Activity
public class MainActivity extends ActionBarActivity { private Button btn = null; private RadioButton rbA = null; // 单选按钮A @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) this.findViewById(R.id.btnChoose); rbA = (RadioButton) this.findViewById(R.id.rbA); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(rbA.isChecked()) { // 当选择是正确的 // 提示 Toast.makeText(MainActivity.this, "所选是正确的", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "所选是错误的", Toast.LENGTH_SHORT).show(); } } }); System.out.println("onCreate"); }}
4. 多选按钮
视图xml文件
activity 文件
public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { // 实现 CompoundButton.OnCheckedChangeListener private CheckBox cb1,cb2,cb3,cb4; // 定义4个多选按钮 private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb1 = (CheckBox) this.findViewById(R.id.cb1); cb2 = (CheckBox) this.findViewById(R.id.cb2); cb3 = (CheckBox) this.findViewById(R.id.cb3); cb4 = (CheckBox) this.findViewById(R.id.cb4); tv = (TextView) this.findViewById(R.id.tvLove); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); cb4.setOnCheckedChangeListener(this); System.out.println("onCreate"); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 当多选按钮变化的时候 String str = "你喜欢"; if (cb1.isChecked()) { str += cb1.getText() + ", "; } if (cb2.isChecked()) { str += cb2.getText() + ", "; } if (cb3.isChecked()) { str += cb3.getText() + ", "; } if (cb4.isChecked()) { str += cb4.getText() + ", "; } tv.setText(str); //改变文本 }}
5. 下拉列表
① 在代码里面生成 下拉的数据
public class MainActivity extends ActionBarActivity{ private static final String[] m = {"A型", "B型", "O型", "AB型", "其他"}; private TextView tv; private Spinner spinner; private ArrayAdapteradapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.spinnerText); spinner = (Spinner) this.findViewById(R.id.spinner); // 将可选内容和 ArrayAdapter 连接 adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, m); // 设置下拉风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 将 adapter添加到apinner中 spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { tv.setText("你的血型是 " + m[position]); } @Override public void onNothingSelected(AdapterView parent) {} }); //设置默认值 spinner.setVisibility(View.VISIBLE); System.out.println("onCreate"); }}
② 下拉框的数据用xml文件
- A型
- B型
- O型
- AB型
- 其他
public class MainActivity extends ActionBarActivity{ private TextView tv; private Spinner spinner; private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.spinnerText); spinner = (Spinner) this.findViewById(R.id.spinner); // 这里创建的adapter的数据是 apinnerArray.xml 里面的数据 adapter = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); // 设置下拉风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 将 adapter添加到apinner中 spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { tv.setText("你的血型是 " + adapter.getItem(position)); // 这里获取值的话 就从adpter中获取 } @Override public void onNothingSelected(AdapterView parent) {} }); //设置默认值 spinner.setVisibility(View.VISIBLE); System.out.println("onCreate"); }}