• 【Android】5.5 状态切换(Switch)和评级条(RatingBar)


    分类:C#、Android、VS2015;

    创建日期:2016-02-07

    一、简介

    1、利用Switch或者ToggleButton切换状态

    如果只有两种状态,可以用ToggleButton控件或Switch控件切换这两种状态。如下图所示(左侧是ToggleButton的效果,右侧是从API 19开始增加的Switch的效果):

    image image

    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、运行效果:

    image

    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();
            }
        }
    }

    运行观察效果。

  • 相关阅读:
    showModalDialog()、showModelessDialog()方法使用详解
    Oracle 8i在GNU/Linux上的安装笔记
    TURBOLinux 7.0下安装Oracle 8.1.7.0.1 release 3
    [Oracle]初识Oracle8i(8.0.5)
    (三)
    退出win不保存设置
    Windows 2000/NT/XP管理员密码丢失解决方法
    DCOM错误:DefaultLaunchPermssion 访问被拒绝
    找到一些工具
    O/R MAP 和 ObjectSpaces 资料
  • 原文地址:https://www.cnblogs.com/rainmj/p/5184556.html
Copyright © 2020-2023  润新知