忽然间就开学了,突然有些不知所措,刚开学的事情乱糟糟的堆在一块,也没有什么心思学习了。 今天课比较少,看了些关于Android的常用控件的知识,整理下来。
下拉列表 在布局文件中使用Spinner控件
1 2 3 4 5 <Spinner         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/spinner"         android:layout_gravity="center_horizontal" /> 
相应程序代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public  class  MainActivity  extends  AppCompatActivity   {    private  Spinner s;      private  String[] dataSource = new  String[]{"IT STUDIO" , "waydrow" , "taylor" };       @Override      protected  void  onCreate (Bundle savedInstanceState)   {         super .onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         s = (Spinner) findViewById(R.id.spinner);           s.setAdapter(new  ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1,dataSource));                    s.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() {             @Override              public  void  onItemSelected (AdapterView<?> parent, View view, int  position, long  id)   {                 System.out.println("用户选择的是 " + dataSource[position]);             }             @Override              public  void  onNothingSelected (AdapterView<?> parent)   { }         });     } } 
展示如下图:
日期选择器 1 2 3 4 5 6 7 8 9 new  DatePickerDialog(ChooseADate.this , new  DatePickerDialog.OnDateSetListener() {	@Override  	public  void  onDateSet (DatePicker view, int  year, int  monthOfYear, int  dayOfMonth)   { 		 		String theDate = String.format("%d-%d-%d" ,year,monthOfYear+1 ,dayOfMonth); 		System.out.println(theDate); 		btnChooseDate.setText(theDate); 	} },2016 ,2 ,30 ).show(); 
非常好看的一个日历控件
时间选择器 和日期选择器类似
1 2 3 4 5 6 7 new  TimePickerDialog(ChooseTime.this , new  TimePickerDialog.OnTimeSetListener() {	@Override  	public  void  onTimeSet (TimePicker view, int  hourOfDay, int  minute)   { 		String time = String.format("%d:%d" ,hourOfDay,minute); 		System.out.println(time); 	} },0 ,0 ,true ).show(); 
单项选择 实现起来非常容易,使用RadioGroup和RadioButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <RadioGroup 	android:layout_width="wrap_content" 	android:layout_height="wrap_content" 	android:layout_gravity="center_horizontal"> 	<RadioButton 	    android:layout_width="wrap_content" 	    android:layout_height="wrap_content" 	    android:text="A" 	    android:id="@+id/rbA" 	    android:layout_gravity="center_horizontal" /> 	<RadioButton 	    android:layout_width="wrap_content" 	    android:layout_height="wrap_content" 	    android:text="B" 	    android:id="@+id/rbB" 	    android:layout_gravity="center_horizontal" /> 	<RadioButton 	    android:layout_width="wrap_content" 	    android:layout_height="wrap_content" 	    android:text="C" 	    android:id="@+id/rbC" 	    android:layout_gravity="center_horizontal" /> 	<RadioButton 	    android:layout_width="wrap_content" 	    android:layout_height="wrap_content" 	    android:text="D" 	    android:id="@+id/rbD" 	    android:layout_gravity="center_horizontal" /> </RadioGroup > 
多项选择 使用CheckBox控件即可
Android常用控件(下拉列表,日期时间选择器,多选单选框)