当前位置:主页 > 移动开发 > Android代码 >

Android中SeekBar拖动条控件使用方法详解

时间:2021-02-06 10:05:15 | 栏目:Android代码 | 点击:

SeekBar拖动条控件使用方法,具体内容如下

一、简介

1、

 二、SeekBar拖动条控件使用方法

1、创建SeekBar控件

<SeekBar 
     android:id="@+id/SeekBar1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:progress="30"
     />

2、添加setOnSeekBarChangeListener监听

seekBar2.setOnSeekBarChangeListener(this); 

三、代码实例

1、效果图:

2、代码

fry.Activity01

package fry;

import com.example.SeekBarDemo1.R;

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

public class Activity01 extends Activity implements OnSeekBarChangeListener{
  private TextView textView1;
  private TextView textView2;
  private SeekBar seekBar1;
  private SeekBar seekBar2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity01);
    textView1=(TextView) findViewById(R.id.TextView1_seekBar);
    textView2=(TextView) findViewById(R.id.TextView2_seekBar);
    seekBar1=(SeekBar) findViewById(R.id.SeekBar1);
    seekBar2=(SeekBar) findViewById(R.id.SeekBar2);
    seekBar1.setOnSeekBarChangeListener(this);
    seekBar2.setOnSeekBarChangeListener(this);
  }
  /*
   * onProgressChanged:进度条改变事件
   * onStartTrackingTouch:进度条开始拖动事件
   * onStopTrackingTouch:进度条停止拖动事件
   * (non-Javadoc)
   * @see android.widget.SeekBar.OnSeekBarChangeListener#onProgressChanged(android.widget.SeekBar, int, boolean)
   */
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    // TODO Auto-generated method stub
    if(seekBar1.getId()==R.id.SeekBar1){
      Toast.makeText(this, "1111111", Toast.LENGTH_SHORT).show();
      textView1.setText("seekBar1当前位置:"+progress);
    }else{
      textView2.setText("seekBar2当前位置:"+progress);
    }
  }
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    if(seekBar1.getId()==R.id.SeekBar1){
      textView1.setText("seekBar1开始拖动");
    }else{
      textView2.setText("seekBar2开始拖动:");
    }
  }
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    if(seekBar1.getId()==R.id.SeekBar1){
      textView1.setText("seekBar1停止拖动");
    }else{
      textView2.setText("seekBar2停止拖动:");
    }
  }
}

/SeekBarDemo1/res/layout/activity01.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_seekBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView1"
    />
   <TextView 
    android:id="@+id/TextView2_seekBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView2"
    />
   
   <SeekBar 
     android:id="@+id/SeekBar1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:progress="30"
     />
   
   <SeekBar 
     android:id="@+id/SeekBar2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:progress="60"
     />



</LinearLayout> 

四、获得

 1、设置初始进度条

android:progress="30"

2、设置setOnSeekBarChangeListener监听

seekBar1.setOnSeekBarChangeListener(this);

3、onProgressChanged:进度条改变事件

4、onStartTrackingTouch:进度条开始拖动事件

5、onStopTrackingTouch:进度条停止拖动事件

您可能感兴趣的文章:

相关文章