分类: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文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:padding="10dp"
  11. android:background="@drawable/android_button" />
  12. <EditText
  13. android:id="@+id/edittext"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content" />
  16. <CheckBox
  17. android:id="@+id/checkbox"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="check it out" />
  21. <RadioGroup
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="vertical">
  25. <RadioButton
  26. android:id="@+id/radio_red"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="Red" />
  30. <RadioButton
  31. android:id="@+id/radio_blue"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="Blue" />
  35. </RadioGroup>
  36. <Switch
  37. android:id="@+id/togglebutton"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="允许开启XX功能码?"
  41. android:checked="true" />
  42. <RatingBar
  43. android:id="@+id/ratingbar"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:numStars="5"
  47. android:stepSize="1.0" />
  48. </LinearLayout>

3、添加Demo06SwitchAndRatingBar.cs文件

  1. using System;
  2. using Android.App;
  3. using Android.OS;
  4. using Android.Views;
  5. using Android.Widget;
  6.  
  7. namespace ch05demos.SrcActivity
  8. {
  9. [Activity(Label = "SwitchAndRatingBarDemo")]
  10. public class Demo06SwitchAndRatingBar : Activity
  11. {
  12. protected override void OnCreate(Bundle savedInstanceState)
  13. {
  14. base.OnCreate(savedInstanceState);
  15. SetContentView(Resource.Layout.demo06_SwitchAndRatingBar);
  16.  
  17. Button button = FindViewById<Button>(Resource.Id.button);
  18. button.Click += delegate
  19. {
  20. Toast.MakeText(this, "Beep Boop", ToastLength.Short).Show();
  21. };
  22.  
  23. var editText = FindViewById<EditText>(Resource.Id.edittext);
  24. //---------------------------------------------------------
  25. //技巧:按+=后,连续按两次<Tab>键,就会自动生成事件处理程序
  26. //---------------------------------------------------------
  27. editText.KeyPress += EditText_KeyPress;
  28.  
  29. var checkbox = FindViewById<CheckBox>(Resource.Id.checkbox);
  30. checkbox.Click += delegate
  31. {
  32. if (checkbox.Checked)
  33. Toast.MakeText(this, "Selected", ToastLength.Short).Show();
  34. else
  35. Toast.MakeText(this, "Not selected", ToastLength.Short).Show();
  36. };
  37.  
  38. var radioRed = FindViewById<RadioButton>(Resource.Id.radio_red);
  39. var radioBlue = FindViewById<RadioButton>(Resource.Id.radio_blue);
  40. radioRed.Click += Radio_Click;
  41. radioBlue.Click += Radio_Click;
  42.  
  43. Switch toggleButton = FindViewById<Switch>(Resource.Id.togglebutton);
  44. toggleButton.Click += (o, e) => {
  45. if (toggleButton.Checked)
  46. Toast.MakeText(this, "Checked", ToastLength.Short).Show();
  47. else
  48. Toast.MakeText(this, "Not checked", ToastLength.Short).Show();
  49. };
  50.  
  51. RatingBar ratingbar = FindViewById<RatingBar>(Resource.Id.ratingbar);
  52. ratingbar.RatingBarChange += (o, e) => {
  53. Toast.MakeText(this, "New Rating: " + ratingbar.Rating.ToString(), ToastLength.Short).Show();
  54. };
  55. }
  56.  
  57. private void EditText_KeyPress(object sender, View.KeyEventArgs e)
  58. {
  59. var editText = sender as EditText;
  60. e.Handled = false;
  61. if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
  62. {
  63. Toast.MakeText(this, editText.Text, ToastLength.Short).Show();
  64. e.Handled = true;
  65. }
  66. }
  67.  
  68. private void Radio_Click(object sender, EventArgs e)
  69. {
  70. RadioButton r = sender as RadioButton;
  71. Toast.MakeText(this, r.Text, ToastLength.Short).Show();
  72. }
  73. }
  74. }

运行观察效果。

【Android】5.5 状态切换(Switch)和评级条(RatingBar)的更多相关文章

  1. Android ToggleButton:状态切换的Button

     Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...

  2. Android APP前后台状态切换

    getActivity().getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleC ...

  3. Android实现监测网络状态

    本文主要用到了安卓监测网络状态变化功能,实现了WIFI,3G,无网络状态切换时发出通知的功能. 主要知识点 service broadcast 接口回调实现 service的基本知识 service可 ...

  4. android 电平信号状态识别View平局

    1.前言 级信号状态View在今天的Android系统是常见.状态的图标就很的经典,有几种状态,到了快没电的时候有些还会闪烁提示用户充电:还有的就是一些地图App的GPS信号强度的提示.Wifi信号强 ...

  5. Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程

    最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...

  6. Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

    原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...

  7. Android菜鸟的成长笔记(15)—— Android中的状态保存探究(下)

    原文:Android菜鸟的成长笔记(15)-- Android中的状态保存探究(下) 在上一篇中我们简单了解关于Android中状态保存的过程和原理,这一篇中我们来看一下在系统配置改变的情况下保存数据 ...

  8. android 获得电池状态

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  9. Android屏幕横竖屏切换和生命周期管理的详细总结

    一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码设置.        今天想学一下Android屏幕横竖屏切换,但是网上很多知识不准确或不正确, ...

随机推荐

  1. eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面

    eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...

  2. Activex打包于发布完整版---ActiveX打包

    前面介绍了数字证书的原理与制作:http://blog.csdn.net/jiangtongcn/article/details/13508365,下面来看一下ActiveX组件的打包. 我现在有一个 ...

  3. 页面可视化编辑ckeditor(web基础学习笔记十五)

    一.CKedit下载ckedit 下载地址:http://ckeditor.com/ 二.ckedit的引入 2.1.解压并将ckedit复制到项目中 2.2.在页面中引入 在页面头部加入 <s ...

  4. Hadoop,HBase集群环境搭建的问题集锦(二)

    10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...

  5. Codeforces 417D Cunning Gena(状态压缩dp)

    题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅 ...

  6. Unity 添加自定义菜单(插件),添加功能

    网上介绍如何写这种插件的文章很多...但是对于新手来说,最基本的,怎么运行这个插件,都不知道...网上的文章都懒得说这个...   幸好,看了半天官方网站别的资料,突然就发现办法了...   这个不是 ...

  7. 判断URL是否支持断点续传?

    #python #xiaodeng #判断URL是否支持断点续传? import urllib2 req = urllib2.Request('http://ftp.ubuntu.com/') req ...

  8. java Socket Tcp 浏览器和服务器(二)

    package cn.itcast.net.p2.ie_server; import java.io.IOException;import java.io.InputStream;import jav ...

  9. keychain实现ssh对秘钥免登陆免输入密码

    Linux同一网段实现密码认证,管理. 项目:https://github.com/funtoo/keychain 01.生成秘钥 ssh-keygen -t rsa   #  -t rsa | ds ...

  10. 【自创+转发】jQuery给input 密码框绑定回车事件

    <script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script&g ...