2swan

Spinner 드롭다운 본문

Programming/Android Function

Spinner 드롭다운

2swan 2023. 8. 5. 12:52

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity3"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:entries="@array/test">
    </Spinner>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="드롭다운 결과"
        android:textSize="35sp"/>
</LinearLayout>

 

main.java

public class MainActivity3 extends AppCompatActivity {

        private Spinner spinner;
        private TextView tv_result;

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

        spinner = (Spinner)  findViewById(R.id.spinner);
        tv_result = (TextView)  findViewById(R.id.tv_result);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                tv_result.setText(adapterView.getItemAtPosition(i).toString());
            }

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

            }
        });
    }
}

 

sub.xml(values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="test">
        <item>바나나</item>
        <item>사과</item>
        <item>포도</item>

    </string-array>

</resources>

결과 값

 

'Programming > Android Function' 카테고리의 다른 글

Radio Button (옵션 선택 버튼)  (0) 2023.08.05
Check box (옵션 선택 버튼)  (0) 2023.08.05
액티비티 생명주기(Life Cycle)  (0) 2023.08.05
WebView  (0) 2023.08.04
Dialog 팝업창  (0) 2023.08.04