Android seekbar实现可拖动进度条

2021-01-29 0 956

本文实例为大家分享了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 seekbar实现可拖动进度条

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

遇见资源网 Android Android seekbar实现可拖动进度条 http://www.ox520.com/23930.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务