Android_ToggleButton,switchRatingBar
XML布局文件:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.app2.MainActivity3" > 10 11 <Switch 12 android:id="@+id/switch1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentRight="true" 17 android:layout_alignParentTop="true" 18 android:layout_marginTop="80dp" 19 android:textOn="打开" 20 android:textOff="关闭" 21 22 /> 23 <!-- 与ToggleButton的功能一样只是样式不一样 --> 24 <ToggleButton 25 android:id="@+id/toggleButton1" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_alignLeft="@+id/switch1" 29 android:layout_alignParentTop="true" 30 android:layout_alignRight="@+id/switch1" 31 android:textOn="开" 32 android:textOff="关" 33 android:checked="true"/> 34 <!-- checked检查是否是打开状态,true为开 --> 35 <RatingBar 36 android:id="@+id/ratingBar1" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:numStars="5" 40 android:stepSize="0.5" 41 android:rating="3" 42 android:isIndicator="false" 43 android:layout_alignLeft="@+id/switch1" 44 android:layout_below="@+id/switch1" 45 android:layout_marginTop="29dp" /> 46 <!-- RatingBar评星 宽度不能设置最大宽度 47 Android:isIndicator RatingBar是否是一个指示器(用户无法修改) 48 --> 49 </RelativeLayout>
源代码:
1 package com.example.app2; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.CompoundButton; 10 import android.widget.CompoundButton.OnCheckedChangeListener; 11 import android.widget.RatingBar; 12 import android.widget.RatingBar.OnRatingBarChangeListener; 13 import android.widget.Switch; 14 import android.widget.Toast; 15 import android.widget.ToggleButton; 16 17 public class MainActivity3 extends Activity { 18 private ToggleButton toggleButton; 19 private Switch switch1; 20 private RatingBar ratingBar; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main3); 25 toggleButton = (ToggleButton) findViewById(R.id.toggleButton1); 26 switch1 = (Switch) findViewById(R.id.switch1); 27 ratingBar = (RatingBar) findViewById(R.id.ratingBar1); 28 toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { 29 30 @Override 31 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 32 if(isChecked) 33 Toast.makeText(MainActivity3.this, "开", Toast.LENGTH_SHORT).show(); 34 else 35 Toast.makeText(MainActivity3.this, "关", Toast.LENGTH_SHORT).show(); 36 37 } 38 }); 39 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 40 41 @Override 42 public void onRatingChanged(RatingBar ratingBar, float rating, 43 boolean fromUser) { 44 // TODO Auto-generated method stub 45 if(fromUser) 46 Toast.makeText(MainActivity3.this, "rating="+rating,Toast.LENGTH_SHORT).show(); 47 } 48 }); 49 } 50 }