【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屏幕横竖屏切换,但是网上很多知识不准确或不正确, ...
随机推荐
- eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面
eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...
- Activex打包于发布完整版---ActiveX打包
前面介绍了数字证书的原理与制作:http://blog.csdn.net/jiangtongcn/article/details/13508365,下面来看一下ActiveX组件的打包. 我现在有一个 ...
- 页面可视化编辑ckeditor(web基础学习笔记十五)
一.CKedit下载ckedit 下载地址:http://ckeditor.com/ 二.ckedit的引入 2.1.解压并将ckedit复制到项目中 2.2.在页面中引入 在页面头部加入 <s ...
- Hadoop,HBase集群环境搭建的问题集锦(二)
10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...
- Codeforces 417D Cunning Gena(状态压缩dp)
题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅 ...
- Unity 添加自定义菜单(插件),添加功能
网上介绍如何写这种插件的文章很多...但是对于新手来说,最基本的,怎么运行这个插件,都不知道...网上的文章都懒得说这个... 幸好,看了半天官方网站别的资料,突然就发现办法了... 这个不是 ...
- 判断URL是否支持断点续传?
#python #xiaodeng #判断URL是否支持断点续传? import urllib2 req = urllib2.Request('http://ftp.ubuntu.com/') req ...
- java Socket Tcp 浏览器和服务器(二)
package cn.itcast.net.p2.ie_server; import java.io.IOException;import java.io.InputStream;import jav ...
- keychain实现ssh对秘钥免登陆免输入密码
Linux同一网段实现密码认证,管理. 项目:https://github.com/funtoo/keychain 01.生成秘钥 ssh-keygen -t rsa # -t rsa | ds ...
- 【自创+转发】jQuery给input 密码框绑定回车事件
<script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script&g ...