【Android】5.5 状态切换(Switch)和评级条(RatingBar)
分类:C#、Android、VS2015;
创建日期:2016-02-07
一、简介
1、利用Switch或者ToggleButton切换状态
如果只有两种状态,可以用ToggleButton控件或Switch控件切换这两种状态。如下图所示(左侧是ToggleButton的效果,右侧是从API 19开始增加的Switch的效果):

2、利用五角星评级条(RatingBar)设置评级
【NumStars】属性:定义星级的个数。
【StepSize】属性:定义每一颗星的粒度(值为 0.5 将允许半星级评级)。
【RatingBarChange】事件:星级发生变化时引发。
例如:
<RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"/>
二、示例6—Demo06SwitchAndRatingBar
1、运行效果:

2、添加demo06_SwitchAndRatingBar.axml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/android_button" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton
android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<Switch
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="允许开启XX功能码?"
android:checked="true" />
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
</LinearLayout>
3、添加Demo06SwitchAndRatingBar.cs文件
using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget; namespace ch05demos.SrcActivity
{
[Activity(Label = "SwitchAndRatingBarDemo")]
public class Demo06SwitchAndRatingBar : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.demo06_SwitchAndRatingBar); Button button = FindViewById<Button>(Resource.Id.button);
button.Click += delegate
{
Toast.MakeText(this, "Beep Boop", ToastLength.Short).Show();
}; var editText = FindViewById<EditText>(Resource.Id.edittext);
//---------------------------------------------------------
//技巧:按+=后,连续按两次<Tab>键,就会自动生成事件处理程序
//---------------------------------------------------------
editText.KeyPress += EditText_KeyPress; var checkbox = FindViewById<CheckBox>(Resource.Id.checkbox);
checkbox.Click += delegate
{
if (checkbox.Checked)
Toast.MakeText(this, "Selected", ToastLength.Short).Show();
else
Toast.MakeText(this, "Not selected", ToastLength.Short).Show();
}; var radioRed = FindViewById<RadioButton>(Resource.Id.radio_red);
var radioBlue = FindViewById<RadioButton>(Resource.Id.radio_blue);
radioRed.Click += Radio_Click;
radioBlue.Click += Radio_Click; Switch toggleButton = FindViewById<Switch>(Resource.Id.togglebutton);
toggleButton.Click += (o, e) => {
if (toggleButton.Checked)
Toast.MakeText(this, "Checked", ToastLength.Short).Show();
else
Toast.MakeText(this, "Not checked", ToastLength.Short).Show();
}; RatingBar ratingbar = FindViewById<RatingBar>(Resource.Id.ratingbar);
ratingbar.RatingBarChange += (o, e) => {
Toast.MakeText(this, "New Rating: " + ratingbar.Rating.ToString(), ToastLength.Short).Show();
};
} private void EditText_KeyPress(object sender, View.KeyEventArgs e)
{
var editText = sender as EditText;
e.Handled = false;
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
Toast.MakeText(this, editText.Text, ToastLength.Short).Show();
e.Handled = true;
}
} private void Radio_Click(object sender, EventArgs e)
{
RadioButton r = sender as RadioButton;
Toast.MakeText(this, r.Text, ToastLength.Short).Show();
}
}
}
运行观察效果。
【Android】5.5 状态切换(Switch)和评级条(RatingBar)的更多相关文章
- Android ToggleButton:状态切换的Button
Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...
- Android APP前后台状态切换
getActivity().getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleC ...
- Android实现监测网络状态
本文主要用到了安卓监测网络状态变化功能,实现了WIFI,3G,无网络状态切换时发出通知的功能. 主要知识点 service broadcast 接口回调实现 service的基本知识 service可 ...
- android 电平信号状态识别View平局
1.前言 级信号状态View在今天的Android系统是常见.状态的图标就很的经典,有几种状态,到了快没电的时候有些还会闪烁提示用户充电:还有的就是一些地图App的GPS信号强度的提示.Wifi信号强 ...
- Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程
最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...
- Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)
原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...
- Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)
原文:Android菜鸟的成长笔记(15)-- Android中的状态保存探究(下) 在上一篇中我们简单了解关于Android中状态保存的过程和原理,这一篇中我们来看一下在系统配置改变的情况下保存数据 ...
- android 获得电池状态
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Android屏幕横竖屏切换和生命周期管理的详细总结
一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码设置. 今天想学一下Android屏幕横竖屏切换,但是网上很多知识不准确或不正确, ...
随机推荐
- sse float 转int 截断和不截断
之前, 我用sse指令, 想把float 型转成int, 不过其中遇到了一些困惑,就是截断和不截断的问题, 这个问题一直困扰我好集体, 最后终于解决了, 原来sse本身就有截断和不截断的指令. _mm ...
- Nginx负载均衡简易方法
做个简单的测试,一个Nginx, 通过FastCGI协议和另外两台服务器上的基于CppCMS开发的web server通信.配置方法很简单: 首先,必须在nginx.conf文件开头,server 配 ...
- SSH框架学习
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- SQL 2008 R2数据库变为REPLICATION,日志不断增长并且不能截断和收缩的解决方式
执行环境:windows server2003,sql server2008 R2,数据库上布置CDC 用户反应系统报错是日志已满,系统不能执行. 查看日志文件时.发现日志文件已经达到15G ...
- sqlserver 创建对某个存储过程执行情况的跟踪
有时候需要抓取执行存储过程时某个参数的值,有时候程序调用存储过程执行后结果不太对,不确定是程序的问题还是存储过程的问题,需要单独执行存储过程看结果 即可用下面的方法 --=============== ...
- javaWeb 批量下载图片
批量下载网页图片 CreateTime--2017年9月26日15:40:43 Author:Marydon 所用技术:javascript.java 测试浏览器:chrome 开发工具:Ecli ...
- C# 获取今天,昨天,上周,下周,上月,下月等等一些日期格式
C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了 今天 DateTime.Now.Date ...
- 转:基于 linux 平台的 libpcap 源代码分析
libpcap 是 unix/linux 平台下的网络数据包捕获函数包,大多数网络监控软件都以它为基础.Libpcap 可以在绝大多数类 unix 平台下工作,本文分析了 libpcap 在 linu ...
- tomcat占用cpu过高解决办法
在工作中经常遇到tomcat占用cpu居高不下,针对这种情况有以下处理办法进行排查. jps --> 查看java的进程 top -Hp pid --> 根据jps得到的进程号(pid), ...
- Linux命令-查看进程命令:pstree
查看进程树,ps aux查看进程,如果进程太多看起来很不方便,可以使用pstree以树形方式显示正在运行的所有进程 pstree -p 查看进程树 还是太多了,可以使用管道符进行查找httpd(apa ...