欢迎来到代码驿站!

Android代码

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

Android seekbar实现可拖动进度条

时间:2021-03-20 10:03:02|栏目:Android代码|点击:

本文实例为大家分享了Android seekbar实现可拖动进度条的具体代码,供大家参考,具体内容如下

SeekBar通过滑块的位置来标识数值 允许用户通过拖动滑块来改变进度值的大小

控件:SeekBar
           两个TextView 显示状态

实现SeekBar.OnSeekBarChangeListener接口 对事件进行监听

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">
 
 <SeekBar
   android:id="@+id/seekBar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:max="100"
   android:progress="50" />
 
 <TextView
   android:id="@+id/tv1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
    />
 
 <TextView
   android:id="@+id/tv2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
 
</LinearLayout>

MainActivity:

package com.example.lenovo.seekbar;
 
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
 
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
 
  private SeekBar seekBar;
  private TextView tv1;
  private TextView tv2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1=findViewById(R.id.tv1);
    tv2=findViewById(R.id.tv2);
    seekBar=findViewById(R.id.seekBar);
    //设置监听器 监听数值改变情况
    seekBar.setOnSeekBarChangeListener(this);
  }
  //数值改变
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    tv1.setText("正在拖动");
    tv2.setText("当前数值:"+progress);
  }
  //开始拖动
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
    tv1.setText("开始拖动");
  }
  //停止拖动
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    tv1.setText("停止拖动");
  }
}

效果图:

上一篇:页面未随软键盘上升及android隐藏软键盘总结

栏    目:Android代码

下一篇:Android发送邮件的方法实例详解

本文标题:Android seekbar实现可拖动进度条

本文地址:http://www.codeinn.net/misctech/84653.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有