从零開始学androidlt;SeekBar滑动组件.二十二.gt;
生活随笔
收集整理的這篇文章主要介紹了
从零開始学androidlt;SeekBar滑动组件.二十二.gt;
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
拖動(dòng)條能夠由用戶自己進(jìn)行手工的調(diào)節(jié),比如:當(dāng)用戶須要調(diào)整播放器音量或者是電影的播放進(jìn)度時(shí)都會(huì)使用到拖動(dòng)條,SeekBar類的定義結(jié)構(gòu)例如以下所看到的: java.lang.Object ?? ? android.view.View ? ?? ? android.widget.ProgressBar ? ? ?? ? android.widget.AbsSeekBar ? ? ? ?? ? android.widget.SeekBar 經(jīng)常用法
public static interface SeekBar.OnSeekBarChangeListener{ /** ?* 開始拖動(dòng)時(shí)觸發(fā)操作 ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?*/ public abstract void onStartTrackingTouch(SeekBar seekBar) ; /** ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?* @param progress 當(dāng)前的進(jìn)度值 ?* @param fromUser 是否為用戶自己觸發(fā) ?*/ public abstract void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) ; /** ?* 停止拖動(dòng)時(shí)觸發(fā)操作 ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?*/ public abstract void onStopTrackingTouch(SeekBar seekBar) ; }
主要的使用
xml文件 <span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><SeekBarandroid:max="100"android:progress="30"android:id="@+id/seekBar1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="60dp" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/seekBar1"android:layout_below="@+id/seekBar1"android:layout_marginLeft="28dp"android:layout_marginTop="32dp"android:text="seek1"android:textAppearance="?android:attr/textAppearanceMedium" /><SeekBarandroid:id="@+id/seekBar2"android:max="100"android:secondaryProgress="60"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/textView1"android:layout_marginTop="67dp" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/seekBar2"android:layout_marginTop="28dp"android:text="seek2"android:textAppearance="?android:attr/textAppearanceMedium" /></RelativeLayout> </span>
java文件
<span style="font-size:18px;">package com.example.seekbar;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 textView1,textView2;private SeekBar seekBar1,seekBar2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView1=(TextView)this.findViewById(R.id.textView1);textView2=(TextView)this.findViewById(R.id.textView2);seekBar1=(SeekBar)this.findViewById(R.id.seekBar1);seekBar2=(SeekBar)this.findViewById(R.id.seekBar2);seekBar1.setOnSeekBarChangeListener(this);seekBar2.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("當(dāng)前seekbar1刻度"+position);}else {textView2.setText("當(dāng)前seekbar2刻度"+position);}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("開始滑動(dòng)seek1");}else {textView2.setText("開始滑動(dòng)seek2");}}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("停止滑動(dòng)seek1");}else {textView2.setText("停止滑動(dòng)seek2");}}} </span>
效果圖
使用seekbar來控制屏幕的亮度
xml文件
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="24dp"android:text="調(diào)節(jié)手機(jī)亮度" /><SeekBarandroid:max="100"android:id="@+id/seekBar1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/textView1"android:layout_marginTop="117dp"android:progress="50" /></RelativeLayout></span>
JAVA文件
<span style="font-size:18px;">package com.example.seekbardemo;import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity implements OnSeekBarChangeListener { private SeekBar myseekBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myseekBar=(SeekBar)this.findViewById(R.id.seekBar1);myseekBar.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stub} //調(diào)節(jié)亮度的方法private void setScreenBrightness(float num) {WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // 取得window屬性layoutParams.screenBrightness = num; // num已經(jīng)除以100super.getWindow().setAttributes(layoutParams); // 0~1之間}@Overridepublic void onStartTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}//在拖動(dòng)結(jié)束是使用getProgress獲得當(dāng)前的Progress值來設(shè)置亮度@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==myseekBar.getId()) { // 將progress除以100并轉(zhuǎn)換為float類型setScreenBrightness((float)seekBar.getProgress()/100);}}} </span>
效果圖
改變后
當(dāng)然,使用SeekBar組件也能夠?qū)σ袅窟M(jìn)行控制,大家能夠查詢相關(guān)API自行嘗試
下節(jié)預(yù)報(bào):RatingBar評(píng)分組件
| public SeekBar(Context context) | 構(gòu)造 | 創(chuàng)建SeekBar類的對(duì)象 |
| public void setOnSeekBarChangeListener( SeekBar.OnSeekBarChangeListener l) | 普通 | 設(shè)置改變監(jiān)聽操作 |
| public synchronized void setMax(int max) | 普通 | 設(shè)置增長(zhǎng)的最大值 |
public static interface SeekBar.OnSeekBarChangeListener{ /** ?* 開始拖動(dòng)時(shí)觸發(fā)操作 ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?*/ public abstract void onStartTrackingTouch(SeekBar seekBar) ; /** ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?* @param progress 當(dāng)前的進(jìn)度值 ?* @param fromUser 是否為用戶自己觸發(fā) ?*/ public abstract void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) ; /** ?* 停止拖動(dòng)時(shí)觸發(fā)操作 ?* @param seekBar 觸發(fā)操作的SeekBar組件對(duì)象 ?*/ public abstract void onStopTrackingTouch(SeekBar seekBar) ; }
主要的使用
xml文件 <span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><SeekBarandroid:max="100"android:progress="30"android:id="@+id/seekBar1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="60dp" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/seekBar1"android:layout_below="@+id/seekBar1"android:layout_marginLeft="28dp"android:layout_marginTop="32dp"android:text="seek1"android:textAppearance="?android:attr/textAppearanceMedium" /><SeekBarandroid:id="@+id/seekBar2"android:max="100"android:secondaryProgress="60"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/textView1"android:layout_marginTop="67dp" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/seekBar2"android:layout_marginTop="28dp"android:text="seek2"android:textAppearance="?android:attr/textAppearanceMedium" /></RelativeLayout> </span>
java文件
<span style="font-size:18px;">package com.example.seekbar;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 textView1,textView2;private SeekBar seekBar1,seekBar2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView1=(TextView)this.findViewById(R.id.textView1);textView2=(TextView)this.findViewById(R.id.textView2);seekBar1=(SeekBar)this.findViewById(R.id.seekBar1);seekBar2=(SeekBar)this.findViewById(R.id.seekBar2);seekBar1.setOnSeekBarChangeListener(this);seekBar2.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("當(dāng)前seekbar1刻度"+position);}else {textView2.setText("當(dāng)前seekbar2刻度"+position);}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("開始滑動(dòng)seek1");}else {textView2.setText("開始滑動(dòng)seek2");}}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("停止滑動(dòng)seek1");}else {textView2.setText("停止滑動(dòng)seek2");}}} </span>
效果圖
使用seekbar來控制屏幕的亮度
xml文件
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="24dp"android:text="調(diào)節(jié)手機(jī)亮度" /><SeekBarandroid:max="100"android:id="@+id/seekBar1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/textView1"android:layout_marginTop="117dp"android:progress="50" /></RelativeLayout></span>
JAVA文件
<span style="font-size:18px;">package com.example.seekbardemo;import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity implements OnSeekBarChangeListener { private SeekBar myseekBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myseekBar=(SeekBar)this.findViewById(R.id.seekBar1);myseekBar.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stub} //調(diào)節(jié)亮度的方法private void setScreenBrightness(float num) {WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // 取得window屬性layoutParams.screenBrightness = num; // num已經(jīng)除以100super.getWindow().setAttributes(layoutParams); // 0~1之間}@Overridepublic void onStartTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}//在拖動(dòng)結(jié)束是使用getProgress獲得當(dāng)前的Progress值來設(shè)置亮度@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==myseekBar.getId()) { // 將progress除以100并轉(zhuǎn)換為float類型setScreenBrightness((float)seekBar.getProgress()/100);}}} </span>
效果圖
改變后
當(dāng)然,使用SeekBar組件也能夠?qū)σ袅窟M(jìn)行控制,大家能夠查詢相關(guān)API自行嘗試
下節(jié)預(yù)報(bào):RatingBar評(píng)分組件
總結(jié)
以上是生活随笔為你收集整理的从零開始学androidlt;SeekBar滑动组件.二十二.gt;的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】mysql-status和vari
- 下一篇: (本地源)安装CDH Manager