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. 第二十四篇、iOS 10版本适配

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ...

  2. Mysql 流程控制

    流程控制 分支结构 if分支结构 语法:     if 条件then         -- 语句体     else         -- 缺省语句体     end if; 示例: 循环结构 whi ...

  3. Java获取线程的对象和名称

    /*获取线程对象以及名称(很有意义的) 原来线程都有自己默认的名称Thread-编号  该编号从0开始 Thread 父类的方法static  Thread currentThread() :获取当前 ...

  4. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  5. makefile--Unfound symbol

    Unfound symbol ,库函数的包含问题或者头文件包含问题 makefile对#的识别度太低了,如果使用了,#它之后的可能就不能识别了,然后会报错的Unfound symbol /////// ...

  6. c++中动态尾随内存的技巧和定位new

    c 和 c++ 最大的特点就是对内存的自由操作,数据类型,其实都是对内存的一种解释方式.C语言中常用的一个技巧就是尾随数据,网络编程中经常会用到这个特性, 特别是以前写完成端口的时候,这个特性肯定是会 ...

  7. VMware10.0.4下 CentOS 6.5 cmake安装 MySQL 5.5.32

    一.准备工作 1.1.创建 zhuzz/tools目录 [root@localhost ~]# mkdir -p /home/zhuzz/tools [root@localhost ~]# cd /h ...

  8. debian 学习记录-5

    后裔排名 1 Debian - 1292 Fedora - 633 Knoppix - 50 (Knoppix本身是Debian后裔) Debian4 SuSE - 28 Debian,由Ian Mu ...

  9. 七牛云覆盖上传 php

    使用七牛云过程中遇到了需要上传覆盖的情况,最终解决,分享给大家. 七牛云sdk上传示例中是这样写的 <?php require_once 'path_to_sdk/vendor/autoload ...

  10. shell写的计算器

    #!/bin/bashif [ $# -ne 3 ] then echo "Usage: $0 num1 + num2" fi case $2 in +) echo $1$2$3= ...