分类: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)的更多相关文章

  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. windows下npm默认的全局路径

    C:\Users\用户名\AppData\Roaming\npm\node_modules

  2. Field.setAccessible()方法

    http://blog.csdn.net/kjfcpua/article/details/8496911 java代码中,常常将一个类的成员变量置为private 在类的外面获取此类的私有成员变量的v ...

  3. Android 如何将Canvas上绘制的内容保存成本地图片(转)

    效果如下图所示 保存在sd卡上的文件为 手机上显示效果为: 1>>在Manifest文件中增加相应权限 <!-- 在SDCard中创建与删除文件权限 --> <uses- ...

  4. Eventually Consistent(最终一致性)(转)

    应该说搞分布式系统必读的文章了,转过来,这是2008年12月Werner revise过的版本,先贴上内容简介:分布式系统的CAP理论 CAP理论(data consistency, system a ...

  5. java 将Map拷贝到另一个Map对象当中

      java 将Map拷贝到另一个Map对象当中 CreateTime--2018年6月4日09点46分 Author:Marydon 1.需求说明 将一个MapA对象中所有的键值对完全拷贝到另一个M ...

  6. 类的专有方法(__init__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.cnblogs.com/zyxstar2003/archive/2011/03/21/ ...

  7. 创建Maven项目后,发现目录里面只有src/main/resources

    创建Maven项目: 创建文件后,发现目录只有一个:缺少了src/main/java 和 src/test/java 百度后发现这个有效果,在本环境中只处理了第三步就可以了: 1.eclipse-&g ...

  8. 全栈JavaScript之路(十三)了解 ElementTraversal 规范

    支持Element Traversal 规范的浏览器有IE 9+.Firefox 3.5+.Safari 4+.Chrome 和Opera 10+. 对于元素间的空格,在IE9之前.都不会返回文档节点 ...

  9. 原创:【微信小程序】客服消息教程(后台以PHP示例)

    1.不需要自己手动开发客服消息的,直接接入客服,不开启消息推送即可.这种模式不多讲. 2.公众号后台开启消息推送模式,配置服务器URL.TOKEN.随机串.数据模式.数据格式(XML或JSON),这个 ...

  10. PHP开发安全问题

    1.不相信表单 对于一般的Javascript前台验证,由于无法得知用户的行为,例如关闭了浏览器的javascript引擎,这样通过POST恶意数据到服务器.需要在服务器端进行验证,对每个php脚本验 ...