【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屏幕横竖屏切换,但是网上很多知识不准确或不正确, ...
随机推荐
- 大型应用的javascript架构
来源:http://blog.leezhong.com/tech/2010/11/29/javascript-arch.html 目前很多网站基本没有明确的前端架构,大多是服务端渲染视图页,输出到浏览 ...
- Java从零开始学四十(反射简述一)
一.JAVA是动态语言吗? 一般而言,说到动态言,都是指在程序运行时允许改变程序结构或者变量类型,从这个观点看,JAVA和C++一样,都不是动态语言. 但JAVA它却有着一个非常突出的动态相关机制:反 ...
- Oracle系统工具包(学习笔记)
Dbms_Output包 No. 子程序名称 描述 1 enable 打开缓冲区,当用户使用 “SET SERVEROUTPUT ON”命令时,自动调用此语句 2 disable 关闭缓冲区,当用户使 ...
- #define中的“#”和“##”的作用
在#define中,标准只定义了#和##两种操作: #用来把参数转换成字符串: ##则用来连接两个前后两个参数,把它们变成一个字符串.
- opencv实现gamma灰阶检測
简单介绍 本篇解说使用opencv来測试,表示camera gamma參数的灰阶卡图片指标:YA Block.DynamicRange.Gray Scale. 详细实现 实现代码 #include & ...
- mysql循环批量插入测试数据
http://blog.51cto.com/tianxingzhe/1676097 DROP PROCEDURE test_insert ; DELIMITER $$ CREATE PROCEDURE ...
- chardet 模块
#coding:utf-8 #指定本文件编码为utf-8 #python 27 #xiaodeng #chardet模块 #chardet模块下载地址: #1)http://pan.baidu.com ...
- JNI 在命令行窗口输入字符,不显所输入字符,显指定的掩饰符
//JNI-命令行窗口输入字符,显掩饰符.txt /* 目标:在命令行窗口输入字符,不显所输入字符,显指定的掩饰符 作者:tangshancheng@21cn.com*/ 1.KeyBoard.j ...
- bash: fork: Resource temporarily unavailable
Last login: Wed Jul 26 09:19:11 2017 from ... -bash: fork: Resource temporarily unavailable -bash-3. ...
- 如何判断微信内置浏览器(JS & PHP)
转自:http://blog.wpjam.com/m/is_weixin/ 为什么要进行判断呢?answer:微信授权登录,微信支付都可以 微信内置浏览器的 User Agent 如何判断微信内置浏览 ...