1.日期选择控件

DatePickerDialog

代码:

         btnChooseDate=(Button) findViewById(R.id.btnChooseDate);
btnChooseDate.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
String theDate=String.format("%d-%d-%d", year,monthOfYear+1,dayOfMonth);
btnChooseDate.setText(theDate);
}
}, 2015, 06, 16).show();
}
});

2.时间选择控件

TimePickerDialog

 btnChooseTime=(Button) findViewById(R.id.btnChooseTime);
btnChooseTime.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String time=String.format("%d-%d", hourOfDay,minute);
btnChooseTime.setText(time);
}
}, 0, 0, true).show();
}
});

3.下拉选择列表

Spinner

 spinner=(Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了"+data[position], Toast.LENGTH_SHORT).show();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
});

4.单选按钮

RadioButton

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

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

资源文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="世界上最大的海洋?"/> <RadioGroup android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/rdA"
android:text="A.太平洋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/rdB"
android:text="B.印度洋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/rdC"
android:text="C.大西洋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/rdD"
android:text="D.喜洋洋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup> <Button android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/> </LinearLayout>

JAVA

 public class SingleChoose extends Activity {

     private Button btnSubmit;
private RadioButton rdA; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.singlechoose);
btnSubmit=(Button) findViewById(R.id.btnSubmit);
rdA=(RadioButton) findViewById(R.id.rdA);
btnSubmit.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (rdA.isChecked()) {
Toast.makeText(SingleChoose.this, "答案是正确的", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(SingleChoose.this, "答案是错误的", Toast.LENGTH_SHORT).show();
}
}
});
} }

5.多选

Checkbox

资源文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:text="请选择你喜欢的事物"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="肉夹馍"/>
<CheckBox android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蛋炒饭"/>
<CheckBox android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鸡蛋面"/>
<CheckBox android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鸡米饭"/>
<TextView android:id="@+id/showResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

JAVA文件

 public class MlutilChoose extends Activity implements OnCheckedChangeListener {

     private CheckBox cb1,cb2,cb3,cb4;
private TextView showResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mlutichoose);
cb1=(CheckBox) findViewById(R.id.cb1);
cb2=(CheckBox) findViewById(R.id.cb2);
cb3=(CheckBox) findViewById(R.id.cb3);
cb4=(CheckBox) findViewById(R.id.cb4);
showResult=(TextView) findViewById(R.id.showResult);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
String str="你喜欢";
if (cb1.isChecked()) {
str+=cb1.getText().toString()+",";
}
if (cb2.isChecked()) {
str+=cb2.getText().toString()+",";
}
if (cb3.isChecked()) {
str+=cb3.getText().toString()+",";
}
if (cb4.isChecked()) {
str+=cb4.getText().toString();
} showResult.setText(str);
}
}

Android开发UI之常用控件的使用的更多相关文章

  1. iOS开发-UI (一)常用控件

    从这里开始是UI篇 知识点: 1.常用IOS基本控件 2.UITouch ======================= 常用基本控件 1.UISegmentedControl:分段控制器 1)创建方 ...

  2. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

  3. iOS开发UI篇—UIScrollView控件介绍

    iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...

  4. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  5. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  6. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  7. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

  8. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  9. 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

    原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...

随机推荐

  1. Objective-C 学习笔记(Day 1)

    -------------------------------------------- Hello World //引入头文件 //c中的引入头文件的方式 //#include <stdio. ...

  2. JavaScript中关于创建对象的笔记

    1,最基本的两种创建对象的方式:构造函数|| 字面量 构造函数: var person = new Object(); person.name = "chen1zee1"; per ...

  3. GNU glibc

    在线G-lib-c(GNU C Library库)网站 参考: 1.bitsToTypes

  4. 堆排序 C++

    1 堆排序拥有插入排序的优点 (是一种原地排序算法只需要存储常数个元素在输入数组以外 即省空间), 同时拥有合并排序算法的复杂度 nlgn,逼格有点高 2 堆数据结构 是一个数组对象,可以被视为一颗完 ...

  5. 九度OJ 1514 数值的整数次方【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1514 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的expo ...

  6. 算法之插入排序(inertionSort)

    插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,适用于少量数据的排序,时间复杂度为O(n^2). 插入排序属于稳定排序. 插入排序不需要构造新的数据 ...

  7. webApi实现增删改查操作

    1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...

  8. tar解压去除文件夹

    tar zxvf test.tar.gz  --strip-components 1 解压到当前目录,并去除一级目录

  9. php class类的用法详细总结

    以下是对php中class类的用法进行了详细的总结介绍,需要的朋友可以过来参考下 一:结构和调用(实例化): class className{} ,调用:$obj = new className(); ...

  10. ECshop网店系统百万级商品量性能优化-简单的一些Cache内存配置

    ECshop网店系统对于产品的数据.模板.Query都可以缓存,也就是把一些商品详情页.分类页.Search页的数据经过一次访问后,用文件的形式保存下来,下次有人访问相同的页面时,不用再查数据库,直接 ...