Android控制之ToggleButton和Switch与RatingBar
2017-09-04 23:20
381 查看
代码演示
Android控制之ToggleButton和Switch与RatingBar
.java文件:
XML布局文件:
Android控制之ToggleButton和Switch与RatingBar
.java文件:
package com.example.togglebuttonandratingbar; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.Switch; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity implements OnCheckedChangeListener,OnRatingBarChangeListener { private ToggleButton toggleButton; private Switch sw; private RatingBar rb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { toggleButton=(ToggleButton) findViewById(R.id.ToggleButton1); toggleButton.setOnCheckedChangeListener(this); sw=(Switch)findViewById(R.id.Switch1); sw.setOnCheckedChangeListener(this); rb=(RatingBar) findViewById(R.id.ratingBar2); rb.setOnRatingBarChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub switch (buttonView.getId()) { case R.id.Switch1: Toast.makeText(getApplicationContext(), sw.getText()+"/"+isChecked, 1000).show(); break; case R.id.ToggleButton1: Toast.makeText(getApplicationContext(), toggleButton.getText()+"/"+isChecked, 1000).show(); } } @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub //如果来自用户选择:我们就Tuast一下星星的数量 if(fromUser) { Toast.makeText(getApplicationContext(),"当前选择的星星数量是:"+rating, 1000).show(); } } }
XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.togglebuttonandratingbar.MainActivity" android:orientation="vertical"> <ToggleButton android:id="@+id/ToggleButton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textOff="关闭" android:textOn="开启" /> <Switch android:id="@+id/Switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是一个Switch" android:textOn="开启" android:textOff="关闭" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是一个指示器你不能选择我:" /> <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="6" android:stepSize="1" android:rating="1" android:isIndicator="true" android:layout_gravity="center"/> <!--设置为 wrap_contentnumStars,numStars才起作用 (numStars星星数量)(stepSize每次选择的不输)(rating当前所选择星星的数量)(isIndicator是否是)--> <RatingBar android:id="@+id/ratingBar2" android:layout_width="wrap_content" 4000 android:layout_height="wrap_content" android:numStars="6" android:stepSize="1" android:layout_gravity="center"/> </LinearLayout>
相关文章推荐
- Android中ToggleButton,Switch,SeekBar,RatingBar的一些用法
- Android控件之ToggleButton、Switch
- Android控件之ProgressBar(进度条),ToggleButton/SwitchBar(开关按钮),SeekBar(拖动条)/RatingBar(等级评分)
- Android零基础入门第21节:ToggleButton和Switch使用大全
- Android控件ToggleButton和Switch
- Android中的ToggleButton和Switch开关控件
- android-UI组件实例大全(五)------开关按钮ToggleButton和开关Switch
- Android基础入门教程——2.3.6 开关按钮ToggleButton和开关Switch
- Android学习路之开关按钮ToggleButton和开关Switch
- Android零基础入门第21节:ToggleButton和Switch使用大全
- ImageView、设定拓展硬件权限(读取内存卡)、ToggleButton、Switch、RatingBar、SeekBar
- android *** Layout 06 RadioButton && ToggleButton && Switch
- android基本控件学习-----ToggleButton&Switch
- 第38章、开关控件Switch和ToggleButton(从零开始学Android)
- Android入门教程二十八之开关按钮ToggleButton和开关Switch
- AndroidUI:ToggleButton + Switch + CheckBox + CheckedTextView
- android基本控件ToggleButton&Switch使用指南
- Android课堂笔记——ToggleButton和Switch
- Android UI控件之ToggleButton、Switch
- android----ToggleButton&Switch