Android开发UI之常用控件的使用
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之常用控件的使用的更多相关文章
- iOS开发-UI (一)常用控件
从这里开始是UI篇 知识点: 1.常用IOS基本控件 2.UITouch ======================= 常用基本控件 1.UISegmentedControl:分段控制器 1)创建方 ...
- [WinForm]WinForm跨线程UI操作常用控件类大全
前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
随机推荐
- C#上传图片和生成缩略图以及图片预览
因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...
- [MAXscript Tool]TimeSlider v1.3
一个简单的小工具,方便在MAX里面快速的修改帧速率,像maya一样.具体看视频演示.
- linux下screen工具的简单使用
有时候,希望即使退出终端了,下次登录linux的时候,还能回到程序的控制界面,这个时候,screen工具就很有用了例如,写了一个从控制台读取屏幕输入的程序input_test,如果从终端退出了,下次登 ...
- jQuery队列控制方法详解queue()/dequeue()/clearQueue()
queue(name,[callback]): 当只传入一个参数时, 它返回并指向第一个匹配元素的队列(将是一个函数数组,队列名默认是fx);$('#demo').queue('name') 当有两 ...
- C# 创建XML文档
有些时候我们需要生成一个xml文档作为数据交换的容器.当然我们用拼接字符串的方法来进行构建xml,但是这种方法虽然简单有效,但是如果xml文档结构过于复杂,拼接字符串会让人眼花缭乱.这时候就需要C#给 ...
- 还原数据库备份文件时,关于“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个。必须提供所有成员”的处理方式
好久没写博客了,最近在做毕设的权限管理模块,今天在还原数据库文件时,遇到了“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个.必须提供所有成员”这个错误 ...
- google map android api v2
我在这主要列举几个需要注意的问题: 1.需要注意使用的api版本的问题,例如google map android api v1就和v2差别很大,包括申请key方面,所以在搜索资料的时候一定注意版本问题 ...
- DevExpress VCL 一键安装工具
一键安装工具 DxAutoInstaller-2.1.3 For Devexpress VCL:http://download.csdn.net/detail/wozengcong/8396181 一 ...
- R中逻辑运算
一.是否相等的判断的方法 (1)判断字符串是否相等is.null(x) (2)判断x的每个元素是否在y中出现: x %in% y (3)判断判断每个相对应的元素是否相等: x == y (4)判断近似 ...
- EF性能优化(一)
一:背景 说到EF的性能问题,我相信都是大家比较头痛的问题,有很多初学者望而却步,可是每每菜鸟在群里面抱怨EF太慢的时候,这个时候总有一些大牛登场说一句:怪EF咯?怪你不会用! 当然我从未嫌弃过它,因 ...