從零開始學android<SeekBar滑動組件.二十二.>

來源:互聯網
上載者:User

標籤:風飛雪未揚   從零開始學android   seekbar組件的使用   android改變螢幕亮度   

拖動條可以由使用者自己進行手工的調節,例如:當使用者需要調整播放器音量或者是電影的播放進度時都會使用到拖動條,SeekBar類的定義結構如下所示:java.lang.Object   ? android.view.View     ? android.widget.ProgressBar       ? android.widget.AbsSeekBar         ? android.widget.SeekBar常用方法
public SeekBar(Context context) 構造 建立SeekBar類的對象
public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener l) 普通 設定改變監聽操作
public synchronized void setMax(int max) 普通 設定增長的最大值

public static interface SeekBar.OnSeekBarChangeListener{/** * 開始拖動時觸發操作 * @param seekBar觸發操作的SeekBar組件對象 */ public abstract void onStartTrackingTouch(SeekBar seekBar) ;/**  * @param seekBar觸發操作的SeekBar組件對象 * @param progress當前的進度值 * @param fromUser是否為使用者自己觸發 */ public abstract void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) ;/** * 停止拖動時觸發操作 * @param seekBar觸發操作的SeekBar組件對象 */ 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" >    <SeekBar        android: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" />    <TextView        android: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" />    <SeekBar        android: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" />    <TextView        android: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;    @Override    protected 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("當前seekbar1刻度"+position);}else {textView2.setText("當前seekbar2刻度"+position);}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("開始滑動seek1");}else {textView2.setText("開始滑動seek2");}}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("停止滑動seek1");}else {textView2.setText("停止滑動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" >    <TextView        android: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="調節手機亮度" />    <SeekBar        android: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}//調節亮度的方法private void setScreenBrightness(float num) {WindowManager.LayoutParams layoutParams = getWindow().getAttributes();// 取得window屬性layoutParams.screenBrightness = num;// num已經除以100super.getWindow().setAttributes(layoutParams); // 0~1之間}@Overridepublic void onStartTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}//在拖動結束是使用getProgress獲得當前的Progress值來設定亮度@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==myseekBar.getId()) {//將progress除以100並轉換為float類型setScreenBrightness((float)seekBar.getProgress()/100);}}}</span>



改變後



當然,使用SeekBar組件也可以對音量進行控制,大家可以查詢相關API自行嘗試





下節預報:RatingBar評分組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.