您的位置:首页 > 移动开发 > Android开发

Android控制之ToggleButton和Switch与RatingBar

2017-09-04 23:20 381 查看
代码演示

Android控制之ToggleButtonSwitchRatingBar

.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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息