1、ToggleButton


在layout中使用:
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:background="@android:color/holo_green_light"
android:textOff="关"
android:textOn="开" />
在activity.java中使用
mToggleButton = findViewById(R.id.togglebutton);
mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(StatisticsActivity1.this, "开", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StatisticsActivity1.this, "关", Toast.LENGTH_SHORT).show();
}
}
});
2、switch


在layout中使用:
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true" />
在activity.java中使用
mSwitch = findViewById(R.id.switch1);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(StatisticsActivity1.this, "开", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StatisticsActivity1.this, "关", Toast.LENGTH_SHORT).show();
}
}
});
3、RatingBar

相关属性:
android:isIndicator:是否用作指示,用户无法更改,默认false
android:numStars:显示多少个星星,必须为整数
android:rating:默认评分值,必须为浮点数
android:stepSize: 评分每次增加的值,必须为浮点数
在layout中使用:
<RatingBar
android:id="@+id/ratingbar"
style="@style/roomRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="3"
android:stepSize="1" />
修改RatingBar的样式:
1、在drawable中新建ratingbat_full.xml
文件,@drawable/gray_xing
是星星图片
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/gray_xing"/>
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/gray_xing"/>
<item android:id="@android:id/progress" android:drawable="@drawable/red_xing"/>
</layer-list>
2、在style文件夹中,添加:
<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar_full</item>
</style>
3、在layout中的ratingBar标签中添加
style="@style/roomRatingBar"
在activity.java中使用
RatingBar ratingbar = findViewById(R.id.ratingbar);
/**
* fromUser 用户点击的
* rating 星星的数量
*/
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if(fromUser){
Toast.makeText(StatisticsActivity1.this, "星星的数量 == " + rating, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(StatisticsActivity1.this, "默认的星星的数量 == " + rating, Toast.LENGTH_SHORT).show();
}
}
});
网友评论