【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屏幕横竖屏切换,但是网上很多知识不准确或不正确, ...
随机推荐
- 算法笔记_196:历届试题 剪格子(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+|10* 1|52|+--****--+|20|30* 1|**** ...
- 安装itunes时提示不能删除旧版本的apple应用程序支持
下载我 选择需要卸载清理的程序(所有与Apple有关的) ok
- ubuntu中设置xampp开机启动
sudo ln -s /opt/lampp/lampp /etc/init.d/lampp sudo update-rc.d -f lampp defaults
- OpenERP的短信(SMS)接口
今天测试了一下OpenERP的短信(SMS)接口. 在OpenERP的Partner界面上,WebClient的右边的工具条有个“send sms”的按钮.OpenERP中发短信用的是短信的Web接口 ...
- java instanceof和isInstance的关系 精析
1.instanceof 用途:判断这个object对象是不是这种Class类型. 语法: boolean result = object instanceof Class; 用法: 判断obje ...
- DIV+CSS专题:十天学会DIV+CSS
DIV+CSS专题:十天学会DIV+CSS,在网上看到的.感觉蛮好,推荐一下. 十天学会DIV+CSS(WEB标准)CHM格式文件下载 第十天 div+css网页标准布局实例教程(三) 第十天 div ...
- OpenWrt设置访客网络Guest Wi-Fi
参考 https://wiki.openwrt.org/doc/recipes/guest-wlan-webinterface 1. 在2.4GHz Wireless Controller下, 创建访 ...
- 关于iOS 类扩展Extension的进一步理解
很多人可能会问 iOS的分类和扩展的区别,网上很多的讲解,但是一般都是分类讲的多,而这也是我们平常比较常用的知识:但是,对于扩展,总觉得理解的朦朦胧胧,不够透彻. 这里就讲一下我自己的理解,但是这个 ...
- 推荐系统之矩阵分解及其Python代码实现
有如下R(5,4)的打分矩阵:(“-”表示用户没有打分) 其中打分矩阵R(n,m)是n行和m列,n表示user个数,m行表示item个数 那么,如何根据目前的矩阵R(5,4)如何对未打分的商品进行评分 ...
- WCF与WPF
1. WCF(Windows Communication Foundation )是一个统一的,可用于建立安全,可靠的面向服务的应用高效的开发平台.WCF是构建安全可靠的事务性服务的统一框架.它是一种 ...