타임피커의 스피너처럼 돌아가는 회전형 커스텀 메뉴를 만들고싶었다.

넘버피커를 활용하면 될거같은데?라고 막연히 생각하던중

딱맞는걸 찾았다 대박적

 

출처는 여기

https://stackoverflow.com/questions/33163828/ios-like-spinner

 

1. 일단 xml에서 넘버피커 하나 만들어주기

<NumberPicker
        android:id="@+id/numberpicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:selectionDividerHeight="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

아래 부분은 디바이더가 꼴보기싫어서 없앤것.. 보기싫어..

android:selectionDividerHeight="0dp"

 

 

2. 해당 액티비티에서 마저 작성하기

	NumberPicker numberpicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound_set);
        
        numberpicker = (NumberPicker) findViewById(R.id.numberpicker);
        //Initializing a new string array with elements
        final String[] values= {"아이템 1","아이템 2", "아이템 3"};

        //Populate NumberPicker values from String array values
        //Set the minimum value of NumberPicker
        numberpicker.setMinValue(0); //from array first value
        //Specify the maximum value/number of NumberPicker
        numberpicker.setMaxValue(values.length-1); //to array last value

        //Specify the NumberPicker data source as array elements
        numberpicker.setDisplayedValues(values);

        //Gets whether the selector wheel wraps when reaching the min/max value.
        numberpicker.setWrapSelectorWheel(true);

        //Set a value change listener for NumberPicker
        numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal){
                //Display the newly selected value from picker
                Toast.makeText(getApplicationContext(), "Selected value : " + values[newVal], Toast.LENGTH_SHORT).show();
            }
        });
    }

 

이러면 됨~! 

 

 

+ Recent posts