๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ์ •๋ ฌํ•˜๋Š” ์…€๋ ‰ํŠธ๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ Spinner ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ์ •๋ ฌํ•˜๋Š” ์…€๋ ‰ํŠธ๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ Spinner

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 27. 18:55
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ ์‹œ ์…€๋ ‰ํŠธ ๋ฐ•์Šค๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž.
๋ช…์นญ์€ ์…€๋ ‰ํŠธ ๋ฐ•์Šค๊ฐ€ ์•„๋‹Œ Spinner์ด๋‹ค.

 

1. xml์—์„œ spinner ์ƒ์„ฑ

<Spinner
    android:id="@+id/spinnerRecipe"
    android:layout_width="80dp"
    android:layout_height="wrap_content"/>

 

2. Activity.java์—์„œ ์„ค์ •

๋จผ์ € Spinner ํƒ€์ž… ๋ณ€์ˆ˜์™€ item Array ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

public class UserRecipeActivity extends AppCompatActivity {

    Spinner spinnerRecipe;
    String[] items = {"์ตœ์‹ ์ˆœ","์˜ค๋ž˜๋œ ์ˆœ","๋ณ„์ ์ˆœ"};
    
...
}

 

๊ทธ๋ฆฌ๊ณ  onCreate์—์„œ ์–ด๋Žํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์„ธํŒ…ํ•œ๋‹ค.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_recipe);

    spinnerRecipe = findViewById(R.id.spinnerRecipe);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item,items);
    adapter.setDropDownViewResource(
      android.R.layout.simple_spinner_dropdown_item);
    spinnerRecipe.setAdapter(adapter);

    spinnerRecipe.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

 

3. ์™„์„ฑ ํ™”๋ฉด

์„ ํƒ ํ›„์—” spinner ์ฐฝ์˜ ํ…์ŠคํŠธ๊ฐ€ ์„ ํƒํ•œ ๋‚ด์šฉ์œผ๋กœ ๋ฐ”๋€๋‹ค!

๋ฐ˜์‘ํ˜•