프로그래밍/안드로이드

안드로이드 - 프로그래스바의 확장형 시크바 (SeekBar) 만들기

가카리 2015. 8. 8. 13:37
반응형

프로그래스바는 현재 위치를 보여주기만 하지만 시크바는 사용자가 직접 값을 조정할 수 있다.

사용자가 시크바를 조정하면 이때마다 OnSeekBarChangedListener 인터페이스의 다음 메서드가 호출된다.

 

void onStartTrackingTouch(SeekBar seekBar)

void onStopTrackingTouch(SeekBar seekBar)

void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)

 

윗 메소드중에 가장 실용적인 것은 onProgressChaged인데 여기서 변경된 위치에 맞게 대상 값을 조정한다.

 

progress 인수는 현재 위치값이며 fromUser인수는 사용자가 직접 드래그해서 변한 것인지 아니면 코드에서 값을 변경한 것인지 알려준다.

 

다음은 xml파일입니다.

activity_test_seekbar.xml

 

<LinearLayout 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:orientation="vertical"

>

 

<SeekBar

android:id="@+id/seekbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="100"

android:progress="50"

android:padding="10dip"

/>

<TextView

android:id="@+id/volume"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Now Volume : 50"

/>

 

</LinearLayout>

 

 

자바파일입니다.

TestSeekBar.java

 

package com.example.ch13_seekbar;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.SeekBar;

import android.widget.TextView;

 

public class TestSeekbar extends Activity {

    SeekBar mSeekBar;

    TextView mVolume;

      

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_seekbar);

        

        //xml 객체를 연결해준다.

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

        mVolume = (TextView)findViewById(R.id.volume);

        

        //시크바에 리스너를 등록한다.

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            

            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            //시크바를 사용자가 조정하면 메소드가 호출된다.

            public void onProgressChanged(SeekBar seekBar, int progress,

                    boolean fromUser) {

                // TODO Auto-generated method stub

            

                mVolume.setText("Now Volume : " + progress);

            }

        });

    }

 

 

      

    

}

 

 

실행화면

 

 

반응형