您的位置:首页技术文章
文章详情页

Android seekbar实现可拖动进度条

浏览:2日期:2022-09-21 08:16:45

本文实例为大家分享了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: android:layout_width='match_parent' android:layout_height='wrap_content' android:max='100' android:progress='50' /> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' /> <TextView android: 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
相关文章: