代码改变世界

SeekBar拖动条控件

2014-03-26 20:22  kingshow  阅读(200)  评论(0编辑  收藏  举报

 

   SeekBar控件可以通过拖动滑竿改变当前的值,可以利用SeekBar来设置有一定范围的变量的值。

一、建立工程,如图

二、activity_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"
        />
    <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>
View Code

三、MainActivity.java中代码

package com.study.seeekbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSeekBarChangeListener{

    private TextView textView1,textView2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView1 = (TextView)this.findViewById(R.id.textview1);
        textView2 = (TextView)this.findViewById(R.id.textview2);
        SeekBar seekBar1 = (SeekBar)this.findViewById(R.id.seekbar1);
        SeekBar seekBar2 = (SeekBar)this.findViewById(R.id.seekbar2);
        
        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //当滑动滑竿的时候,会触发的事件
    @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停止拖动");
        }
        
    }
    
}
View Code

四、效果图

拖动时可以看到值!