安卓课程二十一 SeekBar拖动控件的使用
activity_main.xml
<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" android:layout_width="match_parent" android:layout_height="wrap_content" android:textIsSelectable="true" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" /> <TextView android:id="@+id/textview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textIsSelectable="true" /> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" android:secondaryProgress="60" /> </LinearLayout>
import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity implements OnSeekBarChangeListener{ private TextView tv1,tv2; private SeekBar sb1,sb2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) this.findViewById(R.id.textview1); tv2 = (TextView) this.findViewById(R.id.textview2); sb1 = (SeekBar) this.findViewById(R.id.seekBar1); sb2 = (SeekBar) this.findViewById(R.id.seekBar2); sb1.setOnSeekBarChangeListener(this); sb2.setOnSeekBarChangeListener(this); } /*** * 当滑动滑竿的时候会触发的事件 */ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(seekBar.getId() == R.id.seekBar1){ tv1.setText("seekBar1 当前位置是:"+progress) ; }else { tv2.setText("seekBar2 当前位置是:"+progress) ; } } /** * 当开始拖动滑竿时触发 */ public void onStartTrackingTouch(SeekBar seekBar) { int positon = seekBar.getProgress(); if(seekBar.getId() == R.id.seekBar1){ tv1.setText("开始拖动seekBar1 ,当前位置是:"+positon) ; }else { tv2.setText("开始拖动seekBar2,当前位置是:"+positon) ; } } /** * 当结束拖动滑竿时触发 */ public void onStopTrackingTouch(SeekBar seekBar) { int positon = seekBar.getProgress(); if(seekBar.getId() == R.id.seekBar1){ tv1.setText("结束拖动seekBar1 ,当前位置是:"+positon) ; }else { tv2.setText("结束拖动seekBar2,当前位置是:"+positon) ; } } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28