[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爬虫的知识,新闻数据都是随机生成的字符串...... 但还是很开心 ...
随机推荐
- 快速失败/报错机制 - fail-fast
一.快速报错机制(fail-fast) 这是<Java编程思想>中关于快速报错机制的描述 Java容器有一种保护机制,能够防止多个进程同时修改同一个容器的内容.如果在你迭代遍历容器的过程中 ...
- 【学习总结】Git学习-参考廖雪峰老师教程六-分支管理
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- 社交CRM SCRM
社交CRM - 国际版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=%E7%A4%BE%E4%BA%A4CRM 社交CRM ...
- ShowDoc上手
ShowDoc是什么 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! 程序员都很希望别人能写技术文档,而自己却 ...
- Python3练习题 011:成绩打分
# print('-----判断输入值和60大小判断')# b=int(input('input num'))# if b >60:# print('良')# elif b==60:# prin ...
- [转帖]pfSense软路由系统的使用
图解pfSense软路由系统的使用(NAT功能) http://seanlook.com/2015/04/23/pfsense-usage/ 发表于 2015-04-23 | 更新于: 2015- ...
- js 首次进入弹窗
今天有个需求,首次进入需要弹窗,然后就在网上找了下,虽然看了很多但是说的都不是我想要的,最后终于到了一个合适的. function get_cookie(Name) { var search = Na ...
- ssl证书部署问题
问:我现在得到的ssl证书是.crt和.key两个在nginx环境下部署的证书,如果我们改用是tomcat,现在把这两个文件合成了.jks给tomcat使用,合成的时候输入的jks密码是不是就是部署在 ...
- python数学第四天【古典概型】
- Yii2的使用
yii2的下载安装 使用下载好的文件配置高级模板,在配置文件配置好数据库和gii 在common模板配置db: 在backend模板配置gii: 配置nginx服务器访问backend和fronten ...