zoukankan      html  css  js  c++  java
  • Android香露刀之SeekBar之双管齐下

    传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229


    绿波香露刀 
            《天龙八部》乌老大:他一说完这番话,当即擎鬼头刀在手,绿光一闪,他身旁众人立时闻到“绿波香露刀”的腥臭之气。。。他料想这柄鬼头刀大有来历,但明明臭得厉害,偏偏叫什么“香露刀”,真是好笑。

            今天我们如何利用Android平台“绿波香露刀”SeekBar实现拖动一定范围内的变量值功能,现实生活中经常用于拖动流媒体文件的当前播放进度和显示网络文件加载缓冲区。下面给出该情景的案例:

    一、案例技术要点

    1.SeekBar布局设置
    android:max="100":设置拖动条的最大值为100
    android:progress="30":设置拖动条的一级进度值(初始进度)为30
    android:secondaryProgress="60":设置拖动条的二级进度值(缓存)为60
    2.为SeekBar所在的Activity添加拖动条滑竿改变当前值的监听OnSeekBarChangeListener,并且为OnSeekBarChangeListener引入该监听。
    onStartTrackingTouch(...):滑竿开始滑动时回调该方法
    onStopTrackingTouch(...):滑竿结束滑动时回调该方法

    二、案例代码陈列

    工程包目录


    AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android.seekbar"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".SeekBarMainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    strings.xml
    <resources>
        <string name="app_name">SeekBar拖动滑竿改变当前值</string>
    </resources>
    main.xml
    <?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" >
    
        <TextView
            android:id="@+id/textview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        
        <SeekBar 
            android:id="@+id/seekbar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="30" />
        
        <!-- android:secondaryProgress:标示二级进度(类似缓冲区)-->
        <SeekBar 
            android:id="@+id/seekbar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="30"
            android:secondaryProgress="60" />
    </LinearLayout>
    
    SeekBarMainActivity.java
    package com.android.seekbar;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    /**
     * SeekBar案例:拖动滑竿改变当前值
     * 可以设置一定范围内的变量
     * @author lynnli1229
     */
    public class SeekBarMainActivity extends Activity implements OnSeekBarChangeListener{
        private TextView textView1, textView2;
        private SeekBar seekBar1, seekBar2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            textView1 = (TextView) findViewById(R.id.textview1);
            textView2 = (TextView) findViewById(R.id.textview2);
            seekBar1 = (SeekBar) findViewById(R.id.seekbar1);
            seekBar2 = (SeekBar) findViewById(R.id.seekbar2);
            seekBar1.setOnSeekBarChangeListener(this);
            seekBar2.setOnSeekBarChangeListener(this);
        }
    
        //滑竿滑动时触发
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekBar1的当前位置是:" + progress);
            } else {
                textView2.setText("seekBar2的当前位置是:" + progress);
            }
        }
    
        //从哪儿开始滑动
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            if(seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekBar1的开始滑动");
            } else {
                textView2.setText("seekBar2的开始滑动");
            }
        }
    
        //从哪儿结束滑动
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if(seekBar.getId() == R.id.seekbar1) {
                textView1.setText("seekBar1的结束滑动");
            } else {
                textView2.setText("seekBar2的结束滑动");
            }
        }
    }
    
    三、案例效果展示

     

  • 相关阅读:
    GitHub转华为软件开发云详细教程
    如何将项目管理从禅道迁移到华为软件开发云
    华为CloudIDE免费公测,带你出坑带你飞
    闲谈 | 敏捷宣言说了什么
    Eclipse安装Git插件以及通过Git导入华为软件开发云项目
    终于等到你!MobileTest免费公测,华为带你走出安卓适配大坑
    华为软件开发云测评报告三:测试管理
    华为软件开发云发布管理测评报告
    你真的知道敏捷和迭代吗?
    ThoughtWorks、Teambition、Trello、Slack、DevCloud 主流敏捷软件开发工具平台比较
  • 原文地址:https://www.cnblogs.com/innosight/p/3271187.html
Copyright © 2011-2022 走看看