Android seekbar實現可拖動進度條
本文實例為大家分享了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('停止拖動'); }}
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
