Android學習之SeekBar控制項

來源:互聯網
上載者:User

SeekBar:A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

上面是官方的定義。

什麼是SeekBar控制項,SeekBar控制項其實就是一個進階點的進度條,就像我們在聽歌,看電影用的播放器上的進度條一樣,是可以拖動的,可以改變進度的一個進度條控制項!就是下面這個樣子:

下面來看如何使用SeekBar,用一個例子來說,功能非常簡單,Activity上就是一個SeekBar和一個TextView,當我們拖動SeekBar的進度時,在下面的TextView中顯示相應的進度變化!

第一步:定義Activity  

在main.xml檔案中加上一個SeekBar和一個TextView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

 <SeekBar

  android:id="@+id/seekbar"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

 >

 </SeekBar>

 <EditText

  android:id="@+id/edit"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  >

 </EditText>

</LinearLayout>

第二步:編寫Activity

package com.gufengxiachen.counter;

import android.app.Activity;

import android.os.Bundle;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.widget.TextView;

public class CounterActivity extends Activity {

    /** Called when the activity is first created. */

//定義一個SeekBar和一個TextView

 

private SeekBar seekBar;

private TextView  textView; 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

//根據ID值取得SeekBar對象

        seekBar = (SeekBar)findViewById(R.id.seekbar);

        seekBar.setMax(100);

       //為SeekBar設定監聽器(這裡使用匿名內部類)

        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

//複寫OnSeeBarChangeListener的三個方法

//第一個時OnStartTrackingTouch,在進度開始改變時執行

   @Override

        public void onStartTrackingTouch(SeekBar seekBar) {

        // TODO Auto-generated method stub

       

        }

//第二個方法onProgressChanged是當進度發生改變時執行

        @Override

        public void onProgressChanged(SeekBar seekBar, int progress,

        boolean fromUser) {

        // TODO Auto-generated method stub

        textView = (TextView)findViewById(R.id.edit);

        int i= seekBar.getProgress();

        textView.setText(""+i);

       

        }

//第三個是onStopTrackingTouch,在停止拖動時執行

        @Override

        public void onStopTrackingTouch(SeekBar seekBar) {

        // TODO Auto-generated method stub

        textView = (TextView)findViewById(R.id.edit);

        int i= seekBar.getProgress();

        textView.setText(""+i);

       

        }

     });

    }

}

進過上面簡單的步驟一個簡單的SeekBar應用就完成了,其實其他很多控制項使用方法都一樣,首先定義控制項,然後取得控制項對象,添加監聽器,最後在監聽器的相應方法裡實現功能!!

最後祝大家學習愉快!!!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.