android基本控件学习-----SeekBar&RatingBar

SeekBar(拖动条)和RatingBar(星级评分条)讲解

一、SeekBar(拖动条)

(1)拖动条简单理解就是可以拖动的线,这个对我们来讲很常见,比如视频播放或者音乐播放我们拖动播放的进度,下面总结一些常用属性,很多属性和ProgressBar是一样的
,可以借鉴。

android:max:设置滑动条的最大值

android:progress:表示当前滑动条的值

android:secondaryProgress:二级滑动条

android:thumb:设置滑块的drawable

同样这些属性也可以在Java中调用和设置

(2)SeeBar的监听事件,对监听事件我们应该不会陌生,在j2SE中有一个很重要的知识点监听机制,同样如果学过JavaScript里面也有这样概念叫事件。

onProgressChanged:进度条改变的时候会触发

onStartTrackingTouch:安装SeekBar的时候会触发

OnStopTrackingTouch:放开SeekBar的时候会触发

(3)简单实例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <SeekBar
        android:id="@+id/sb1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:max="100"
        android:thumb="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:text="Seek的当前值是0"
        android:textSize="24sp"/>
</LinearLayout>

Java代码

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity{
    private  TextView textView;
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.sb1);
        textView = (TextView) findViewById(R.id.tv1);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                textView.setText("SeekBar的当前值为" + i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this,"你按住了SeekBar",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this,"你松开了SeekBar",Toast.LENGTH_LONG).show();
            }
        });
    }
}

(4)定制SeekBar

step1:在drawable定义一个stateListDrawable文件命名为sb_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"  android:drawable="@mipmap/press"/>
    <item android:state_pressed="false" android:drawable="@mipmap/nomal"/>
</selector>

step2:在drawable定义一个layer-list,这时层叠图片这里是背景和当前进度条,命名为sb_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#FFFFD042" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#FF96E85D" />
            </shape>
        </clip>
    </item>
</layer-list>

step3:在布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <SeekBar
        android:id="@+id/sb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progressDrawable="@drawable/sb_bar"
        android:thumb="@drawable/sb_thumb"/>
</LinearLayout>

效果图:

二、RatingBar(星级评分条)

(1)星级评分条从这个名词上面我们就很容易想象它的效果图,在网上面买东西,经常就有店家让我们给5星评价,下面总结一下常用的属性:

android:isIndicator:是否用作指示,用户无法更改,默认是false

android:numStars:显示多少个星星,必须是整数

android:rating:默认是评分值,必须是浮点数

android:stepSize:评分每次增加的值,必须是浮点数style:样式系统提供可选的有?android:attr/ratingBarStyleSmall和?android:attr/ratingBarStyleIndicator

(2)事件处理:只需要为RatinBar设置OnRatingBarChangeListener即可

(3)简单实例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <RatingBar
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:rating="3.5"/>
</LinearLayout>

(4)定制RatingBar

step1:和前面的SeekBar一样编写一个layer-list的文件:ratingbar_full.xml:

<?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="@mipmap/press"/>
    <item android:id="@android:id/progress" android:drawable="@mipmap/nomal"/>
</layer-list>

step2:在style文件中使用

step3:在布局文件使用定义的style

效果图

posted @ 2016-01-11 17:34  会飞的一只狼  阅读(687)  评论(0编辑  收藏  举报