[2017-7-28]Android Learning Day6
常用控件
spinner(下拉菜单)
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
public class MainActivity extends AppCompatActivity {
private Spinner s;
private String[] dataSource = new String[]{"a1","b2","c3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s = (Spinner) findViewById(R.id.spinner);
//设置一个Adapter
s.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataSource));
//sOnSe OnIt
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) {
//不写
}
});
}
}
DatePickerDialog(日期选择器)
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择日期" />
public class ChooseDate extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_date);
slove();
}
public void slove() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(ChooseDate.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String chooseDate = String.format("%04d-%02d-%02d",year,month+1,dayOfMonth);//"%04d"总长度4位,不够用0补齐
((Button) findViewById(R.id.button)).setText(chooseDate);
}
},2000,0,1).show();//要注意月份是从0~11
}
});
}
}
TimePickerDialog(时间选择器)
这个就和日期选择器很像了
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择时间" />
public class ChooseTime extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_date);
slove();
}
public void slove() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TimePickerDialog(ChooseTime.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String chooseTime = String.format("%02d:%02d",hourOfDay,minute);
((Button) findViewById(R.id.button)).setText(chooseTime);
}
},0,0,true).show();//True是否使用24小时制
}
});
}
}
RadioButton(单选框)
这个要注意的是,单选按钮不能单独存在,要放在RadioGroup里
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:text="世界上最大的海洋是?"/> <RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" > <RadioButton
android:id="@+id/rbA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="A.太平洋" /> <RadioButton
android:id="@+id/rbB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="B.北冰洋" />
</RadioGroup> </LinearLayout> <Button
android:id="@+id/chooseOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/> </LinearLayout>
public class ChooseSingle extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_single);
slove();
}
private void slove() {
findViewById(R.id.chooseOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) findViewById(R.id.rbA);
if(rb.isChecked()) {
Toast.makeText(ChooseSingle.this, "正确", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(ChooseSingle.this, "错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
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:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你喜欢喝什么饮料?" /> <CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="冰峰" /> <CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="果啤" /> <CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="雪碧" /> <TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.liwenchi.myapplication; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private CheckBox cb1,cb2,cb3;
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slove();
} public void slove() {
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
tv = (TextView) findViewById(R.id.tv2);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
} public void onChanged() {
String s = "我喜欢";
if(cb1.isChecked()) {
s += ","+cb1.getText();
}
if(cb2.isChecked()) {
s += ","+cb2.getText();
}
if(cb3.isChecked()) {
s += ","+cb3.getText();
}
tv.setText(s);
}
}
[2017-7-28]Android Learning Day6的更多相关文章
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.28
2017.04.28 天气晴朗 东风3级. 时间:上午 9:35 ---10:10分 地点:陆大二楼 会议内容:实验室报修系统项目冲刺Alpha版的的最后一天,大家对现在项目的进程进行了讨论,阐述了各 ...
- LOJ #6074. 「2017 山东一轮集训 Day6」子序列
#6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...
- [2017-8-02]Android Learning Day8
自定义动画效果 新建一个customAnim类 package com.liwenchi.myapplication; import android.view.animation.Animation; ...
- [2017-7-26]Android Learning Day4
RecycleView 恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在.(现在也还是一头雾水,不过照搬也会用了) 这是第一版的代 ...
- Android Learning:微信第三方登录
这两天,解决了微信第三方授权登录的问题,作为一个新手,想想也是一把辛酸泪.我想着,就把我的遇到的坑给大家分享一下,避免新手遇到我这样的问题能够顺利避开. 步骤一 微信开发者平台 我开始的解决思路是,去 ...
- Android Learning:多线程与异步消息处理机制
在最近学习Android项目源码的过程中,遇到了很多多线程以及异步消息处理的机制.由于之前对这块的知识只是浅尝辄止,并没有系统的理解.但是工程中反复出现让我意识到这个知识的重要性.所以我整理出这篇博客 ...
- Android Learning:数据存储方案归纳与总结
前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...
- [2017-7-28]Android Learning Day7
View动画效果 透明动画效果 旋转动画效果 移动动画效果 缩放动画效果 混合动画效果 1.透明动画效果(AlphaAnimation) 有两种方法 第一种在活动中设置,不需要xml文件 public ...
- [2017-7-27]Android Learning Day5
总结篇! 吭哧吭哧了三天,最近不断研究<第一行代码:第二版>170多页的那个新闻实践项目,虽然也没有用到数据库和一些Web爬虫的知识,新闻数据都是随机生成的字符串...... 但还是很开心 ...
随机推荐
- 使用Browser请求 和 在cli中运行程序的分析
在browser中请求后端的一个程序之后,立马将窗口关闭,服务器端的程序是否会挂起或者终止,还是继续执行? 如果是继续执行,那么执行完毕后,结果去了哪里?-->apache的工作流程 使用Bro ...
- 【学习总结】C-翁恺老师-入门-第4周<循环控制>
[学习总结]C-翁恺老师-入门-总 1-阶乘:引入for循环 2-控制循环次数:初始化与控制条件的设置 任何一个for循环都可以写成一个while循环 for中的每一个表达式都是可以省略的:for(; ...
- java kill thread command line
multithreading - How do you kill a Thread in Java? - Stack Overflowhttps://stackoverflow.com/questio ...
- Windows10常用快捷键
1. 打开注册表 ctrl+R ---> regedit 2.打开资源管理器 win + E 3.切换到桌面 win + D 再按一次可以进行还原 4.锁屏 win+ ...
- [转帖]Windows DHCPServer远程代码执行漏洞分析(CVE-2019-0626)
Windows DHCPServer远程代码执行漏洞分析(CVE-2019-0626) ADLab2019-03-15共23605人围观 ,发现 4 个不明物体安全报告漏洞 https://www.f ...
- MHA高可用及读写分离
一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 七.配置VIP漂移 八.配置binlog-server 九.MySQL中间件Atlas
- H5的使用
1.h5的部分标签浏览器兼容性问题 解决方案: <head> <!--[if lt IE 9]> <script src="http://cdn.static ...
- Yii2的使用
yii2的下载安装 使用下载好的文件配置高级模板,在配置文件配置好数据库和gii 在common模板配置db: 在backend模板配置gii: 配置nginx服务器访问backend和fronten ...
- zookeeper客户端操作
ZooKeeper客户端 zkCli.sh 节点的增删改查 在 bin 目录下的 zkCli.sh 就是ZooKeeper客户端 ./zkCli.sh -timeout 5000 -server ...
- Vivado安装、生成bit文件及烧录FPGA的简要流程
https://wenku.baidu.com/view/0294cbb3bb4cf7ec4bfed01a.html