publicclassMainActivityextendsActivityimplementsRunnable{privateThreadtd;privateProgressBarpb;privatebooleanstateChange;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pb=(ProgressBar)findViewById(R.id.pd);td=newThread(this);td.start();}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main,menu);returntrue;}@Overridepublicvoidrun(){// TODO Auto-generated method stubwhile(true){intcurrent=pb.getProgress();intmax=pb.getMax();intsecurrent=pb.getSecondaryProgress();if(stateChange==false){if(current>=max){stateChange=true;}else{pb.setProgress(current+1);pb.setSecondaryProgress(current+1);}}else{if(current<=0){stateChange=false;}else{pb.setProgress(current-1);pb.setSecondaryProgress(current-1);}}try{td.sleep(50);}catch(Exceptione){e.printStackTrace();}}}}
6、seekBar
拖动条,见到的较多,如地图上,放大缩小,音乐播放时声音的放大缩小等
监听事件:
123456789101112131415161718
@OverridepublicvoidonStopTrackingTouch(SeekBarseekBar){// TODO Auto-generated method stubtv.setText("拖动完成...");}@OverridepublicvoidonStartTrackingTouch(SeekBarseekBar){// TODO Auto-generated method stubtv.setText("拖动中...");}@OverridepublicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){// TODO Auto-generated method stubtv.setText("当前拖动条值为:"+progress);}