android之seekBar
16lz
2021-12-20
效果:
layout中的xml文件:
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="当前进度:"/>
- <SeekBar
- android:id="@+id/mySeekBar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="30"
- />
- </LinearLayout>
Activaty.java文件:
- packagecom.cheng.seekbarproject;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.view.Menu;
- importandroid.widget.SeekBar;
- importandroid.widget.Toast;
- importandroid.widget.SeekBar.OnSeekBarChangeListener;
- importandroid.widget.TextView;
- publicclassSeekBarActivityextendsActivity{
- //定义组件
- privateTextViewmTextView;
- privateSeekBarmSeekBar;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- //获得组件
- mTextView=(TextView)findViewById(R.id.myTextView);
- mSeekBar=(SeekBar)findViewById(R.id.mySeekBar);
- //设置进度条的监听器
- OnSeekBarChangeListenerosbcl=newOnSeekBarChangeListener(){
- @Override
- publicvoidonStopTrackingTouch(SeekBarseekBar){
- //TODOAuto-generatedmethodstub
- //当鼠标抬起的时候触发迟事件
- Toast.makeText(getApplicationContext(),"onStopTrackingTouch",Toast.LENGTH_LONG).show();
- }
- @Override
- publicvoidonStartTrackingTouch(SeekBarseekBar){
- //TODOAuto-generatedmethodstub
- //当鼠标点击下的时候触发该事件
- Toast.makeText(getApplicationContext(),"onStartTrackingTouch",Toast.LENGTH_LONG).show();
- }
- @Override
- publicvoidonProgressChanged(SeekBarseekBar,intprogress,
- booleanfromUser){
- //TODOAuto-generatedmethodstub
- //当进度条的值发生改变的时候触发
- mTextView.setText("当前进度:"+progress);
- Toast.makeText(getApplicationContext(),"当前进度:"+progress+"%",Toast.LENGTH_LONG).show();
- }
- };
- //绑定监听
- mSeekBar.setOnSeekBarChangeListener(osbcl);
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- //Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
- getMenuInflater().inflate(R.menu.main,menu);
- returntrue;
- }
- }
更多相关文章
- Android使用GPS定位
- Android为Notification加上一个进度条
- Android(安卓)密度转换多分辨率
- Android(安卓)判断是否为Wifi网络
- android 判断当前网络是否可用
- android 如何判断当前进程在主页面
- Android(安卓)BaseAdapter如何获得每一项并添加按钮事件
- android卸载应用的进度条
- android上方显示进度的进度条