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

Android开发-ToggleButton-Switch-RatingBar-AndroidStudio

2016-12-12 17:15 411 查看
转载请注明出处:http://blog.csdn.net/iwanghang/

我正在参加CSDN 2016博客之星评选,希望得到您的宝贵一票~
http://blog.csdn.net/vote/candidate.html?username=iwanghang
请为我投票,谢谢 ~~
如果没有CSDN账号 直接使用微信/QQ/微博登陆 就能投票了~~

直接看GIF效果图和代码:



MainActivity.java:
Public class MainActivity extends AppCompatActivity {

private ToggleButton toggleButton;
private Switch switch1;
private RatingBar ratingBar;
private ImageView imageView;

public static final String TAG = "MainActivity";

private DynamicButton mButton;
private DynamicButton mButtonTwo;
private ProgressButton mProgressButton;
private int value = 1;
private Timer mTimer = new Timer();
int progress = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
switch1 = (Switch) findViewById(R.id.switch1);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
imageView = (ImageView) findViewById(R.id.imageView);

toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
Toast.makeText(MainActivity.this, "开", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.off);
}else {
Toast.makeText(MainActivity.this, "关", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.on);
}
}
});

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
}
});

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
if (b){
Toast.makeText(MainActivity.this, "当前评分为" + v + "颗星", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.iwanghang.buttondemo.MainActivity">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="这是一个ToggleButton:
\n text属性无效 只可以设置textOn/textOff"/>

<ImageView
android:id="@+id/imageView"
android:src="@drawable/off"
android:layout_width="wrap_content"
android:layout_height="225dp"
android:layout_centerHorizontal="true" />

<ToggleButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:textOn="开"
android:textOff="关"
android:checked="true"
android:id="@+id/toggleButton" />

<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />

<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="这是一个Switch:
\n text属性在左边显示 也可以设置textOn/textOff"/>

<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:textOn="确定"
android:textOff="取消" />

<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />

<TextView
android:id="@+id/tv_3"
android:layout_below="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="这是一个RatingBar:
\n numStars属性:星星数量
\n rating属性:默认选中数量
\n stepSize属性:步长
\n isIndicator属性:为true时,表示为指示器,不能选择几颗星"/>

<RatingBar
android:id="@+id/ratingBar"
android:isIndicator="false"
android:layout_below="@+id/tv_3"
android:numStars="5"
android:rating="3"
android:stepSize="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />

</LinearLayout>
</ScrollView>
</RelativeLayout>

这里是资源文件:





转载请注明出处:http://blog.csdn.net/iwanghang/

欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式



微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com

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