Android入门——UI(2)
介绍SeekBar拖动条控件、ProgressBar进度条控件、DatePicker日历控件、TimePicker时间控件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"> <SeekBar
android:id="@+id/my_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="200"
android:thumb="@mipmap/ic_launcher" /> <TextView
android:id="@+id/tv_seek_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="0" /> <ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="45" /> <Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" /> <Button
android:id="@+id/btn_dec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减小" /> <DatePicker
android:id="@+id/my_date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></DatePicker> <TimePicker
android:id="@+id/my_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TimePicker> <TextView
android:id="@+id/show_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
seekbar_index.xml
布局代码相对简单,主要介绍事件。
package com.ouc.wkp.ui1; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker; import java.sql.Time; /**
* Created by wkp on 2016/8/23.
*/
public class JustLook2 extends Activity { SeekBar mySeekBar;
TextView tvSeekBarValue; ProgressBar progressBar; DatePicker datePicker;
TimePicker timePicker;
TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar_index); tvSeekBarValue = (TextView) findViewById(R.id.tv_seek_bar);
mySeekBar = (SeekBar) findViewById(R.id.my_seek_bar);
mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
tvSeekBarValue.setText(String.valueOf(i - 100));
} @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) { }
}); progressBar = (ProgressBar) findViewById(R.id.progress);
findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setProgress(progressBar.getProgress() + 1);
}
}); findViewById(R.id.btn_dec).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setProgress(progressBar.getProgress() - 1);
}
}); datePicker = (DatePicker) findViewById(R.id.my_date_picker);
timePicker = (TimePicker) findViewById(R.id.my_time_picker);
textView = (TextView) findViewById(R.id.show_time); datePicker.init(this.datePicker.getYear(), this.datePicker.getMonth(), this.datePicker.getDayOfMonth(), new OnDateChangedListenerImpl()); timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int i, int i1) {
setDateTime();
}
});
} public void setDateTime() {
String date = String.valueOf(datePicker.getYear() + "-" + datePicker.getMonth() + "-" + datePicker.getDayOfMonth() + " ");
String time = String.valueOf(timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute());
textView.setText(date + time);
} private class OnDateChangedListenerImpl implements DatePicker.OnDateChangedListener { @Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
JustLook2.this.setDateTime();
}
}
}
JustLook2.java
写的有点乱
一、我们通过id获取seekbar控件后,可以添加改变事件

参数i代表当前进度数值,运行效果如下

二、我们可以通过按钮改变progressbar的进度

运行效果

三、TimePicker和DatePicker
注意我们需要写一个类实现onDataChangeListener

再写一个方法来给TextView赋值

然后给timepicker和datepicker添加事件

注意这行 添加事件监听的形式不一样
datePicker.init(this.datePicker.getYear(), this.datePicker.getMonth(), this.datePicker.getDayOfMonth(), new OnDateChangedListenerImpl());
运行效果

点击日历或钟表后左下角文本改变
Android入门——UI(2)的更多相关文章
- Android入门——UI(8)——Fragment(2)
先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- Android入门——UI(7)——Fragment
先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- android入门——UI(6)——ViewPager+Menu+PopupWindow
一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...
- android入门——UI(5)
最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- android入门——UI(3)
Spinner控件 ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
随机推荐
- 在Word中为标题样式添加自动编号功能
原文地址:http://blog.chinaunix.net/uid-16685753-id-2738270.html 摘要: 本文可以帮助你在Office 2007中为Word标题样式添加和设置自动 ...
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...
- How to change a product dropdown attribute to a multiselect in Magento
First, update the attribute input type to multiselect: UPDATE eav_attribute SET entity_type_id ', at ...
- Spring中自己主动装配
自己主动装配 在我们了解过constructor-arg和property装配中.都须要配置对应的属性和值或者引用,假设在比較复杂的项目中.就会使得XML的配置变得复杂,自己主动装配能够使用较少的配置 ...
- foreach真的比for性能高吗
void Main() { ; List<int> list=new List<int>(); ;i<count;i++) { list.Add(i); } List&l ...
- SharePoint解决方案由VS2010升级到VS2013部署页面报错
“/”应用程序中的服务器错误.----------------------------------------文件“/_CONTROLTEMPLATES/....../XXXUserControl.a ...
- 图片处理之-Bitmap.Config,jpeg压缩与大小
关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...
- Programming C#.Interfaces
类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定.” 从抽象类继承实现了“is-a(是一种)”关系,实现接口时一种“implement ...
- JVM学习之GC常用算法
出处:博客园左潇龙的技术博客--http://www.cnblogs.com/zuoxiaolong,多谢分享 GC策略解决了哪些问题? 既然是要进行自动GC,那必然会有相应的策略,而这些策略解决了哪 ...
- mysql utf8_bin跟utf8_general_ci的区别
ci是 case insensitive, 即 "大小写不敏感", a 和 A 会在字符判断中会被当做一样的; bin 是二进制, a 和 A 会别区别对待. 例如你运行: SEL ...