码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之常用控件的使用

时间:2015-07-14 17:51:30      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

1.日期选择控件

DatePickerDialog

代码:

 1         btnChooseDate=(Button) findViewById(R.id.btnChooseDate);
 2         btnChooseDate.setOnClickListener(new OnClickListener() {
 3             
 4             @Override
 5             public void onClick(View v) {
 6                 // TODO Auto-generated method stub
 7                 new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
 8                     
 9                     @Override
10                     public void onDateSet(DatePicker view, int year, int monthOfYear,
11                             int dayOfMonth) {
12                         // TODO Auto-generated method stub
13                         String theDate=String.format("%d-%d-%d", year,monthOfYear+1,dayOfMonth);
14                         btnChooseDate.setText(theDate);
15                     }
16                 }, 2015, 06, 16).show();
17             }
18         });

2.时间选择控件

TimePickerDialog

 1 btnChooseTime=(Button) findViewById(R.id.btnChooseTime);
 2         btnChooseTime.setOnClickListener(new OnClickListener() {
 3             
 4             @Override
 5             public void onClick(View v) {
 6                 // TODO Auto-generated method stub
 7                 new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
 8                     
 9                     @Override
10                     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
11                         // TODO Auto-generated method stub
12                         String time=String.format("%d-%d", hourOfDay,minute);
13                         btnChooseTime.setText(time);
14                     }
15                 }, 0, 0, true).show();
16             }
17         });

3.下拉选择列表

Spinner

 1 spinner=(Spinner) findViewById(R.id.spinner1);
 2         spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
 3         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
 4 
 5             @Override
 6             public void onItemSelected(AdapterView<?> parent, View view,
 7                     int position, long id) {
 8                 // TODO Auto-generated method stub
 9                 Toast.makeText(MainActivity.this, "点击了"+data[position], Toast.LENGTH_SHORT).show();
10             }
11 
12             @Override
13             public void onNothingSelected(AdapterView<?> parent) {
14                 // TODO Auto-generated method stub
15                 
16             }
17         });

4.单选按钮

RadioButton

注意:在XML文件中,必须使用RadioGroup控件,才可以使用RadioButton。

在JAVA文件中,判断RadioButton是否被点击,使用方法isChecked()。

资源文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="世界上最大的海洋?"/>
 9     
10     <RadioGroup android:id="@+id/radioGroup"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content">
13         <RadioButton android:id="@+id/rdA"
14             android:text="A.太平洋"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"/>
17         <RadioButton android:id="@+id/rdB"
18             android:text="B.印度洋"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"/>
21         <RadioButton android:id="@+id/rdC"
22             android:text="C.大西洋"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"/>
25         <RadioButton android:id="@+id/rdD"
26             android:text="D.喜洋洋"
27             android:layout_width="wrap_content"
28             android:layout_height="wrap_content"/>
29     </RadioGroup>
30     
31     <Button android:id="@+id/btnSubmit"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:text="提交"/>
35 
36 </LinearLayout>

JAVA

 1 public class SingleChoose extends Activity {
 2     
 3     private Button btnSubmit;
 4     private RadioButton rdA;
 5     
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         // TODO Auto-generated method stub
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.singlechoose);
11         btnSubmit=(Button) findViewById(R.id.btnSubmit);
12         rdA=(RadioButton) findViewById(R.id.rdA);
13         btnSubmit.setOnClickListener(new OnClickListener() {
14             
15             @Override
16             public void onClick(View v) {
17                 // TODO Auto-generated method stub
18                 if (rdA.isChecked()) {
19                     Toast.makeText(SingleChoose.this, "答案是正确的", Toast.LENGTH_SHORT).show();
20                 }
21                 else {
22                     Toast.makeText(SingleChoose.this, "答案是错误的", Toast.LENGTH_SHORT).show();
23                 }
24             }
25         });
26     }
27 
28 }

5.多选

Checkbox

资源文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView android:text="请选择你喜欢的事物"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"/>
 9     <CheckBox android:id="@+id/cb1"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="肉夹馍"/>
13     <CheckBox android:id="@+id/cb2"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="蛋炒饭"/>
17     <CheckBox android:id="@+id/cb3"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="鸡蛋面"/>
21     <CheckBox android:id="@+id/cb4"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:text="鸡米饭"/>
25     <TextView android:id="@+id/showResult"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"/>
28 </LinearLayout>

JAVA文件

 1 public class MlutilChoose extends Activity implements OnCheckedChangeListener {
 2     
 3     private CheckBox cb1,cb2,cb3,cb4;
 4     private TextView showResult;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         // TODO Auto-generated method stub
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.mlutichoose);
10         cb1=(CheckBox) findViewById(R.id.cb1);
11         cb2=(CheckBox) findViewById(R.id.cb2);
12         cb3=(CheckBox) findViewById(R.id.cb3);
13         cb4=(CheckBox) findViewById(R.id.cb4);
14         showResult=(TextView) findViewById(R.id.showResult);
15         cb1.setOnCheckedChangeListener(this);
16         cb2.setOnCheckedChangeListener(this);
17         cb3.setOnCheckedChangeListener(this);
18         cb4.setOnCheckedChangeListener(this);
19     }
20     @Override
21     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
22         // TODO Auto-generated method stub
23         String str="你喜欢";
24         if (cb1.isChecked()) {
25             str+=cb1.getText().toString()+",";
26         }
27         if (cb2.isChecked()) {
28             str+=cb2.getText().toString()+",";
29         }
30         if (cb3.isChecked()) {
31             str+=cb3.getText().toString()+",";
32         }
33         if (cb4.isChecked()) {
34             str+=cb4.getText().toString();
35         }
36 
37         showResult.setText(str);
38     }
39 }

 

Android开发之常用控件的使用

标签:

原文地址:http://www.cnblogs.com/liyiran/p/4645854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!