Android 스튜디오 (Java)에서 spinner로 문자 배열을 표시하는 소스

11730 단어 AndroidStudiojava8
자신의 메모입니다.
타이틀대로입니다만, 문자 배열을 정의해 두고, spinner(드롭다운 메뉴)로, 표시시켜 그것을 텍스트(TextView)에 표시하는 소스입니다.



MainActivity.java
package com.example.ktakeda.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    TextView resultTextView;

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

        findViews();
        initViews();

    }

    private void findViews() {
        // objectを取得
        spinner = (Spinner)findViewById(R.id.spinner);
        resultTextView = (TextView)findViewById(R.id.result);
    }

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

                // 選択されているアイテムのIndexを取得
                int idx = (int)spinner.getSelectedItemPosition();
                Log.d("idx", "idx:" + idx);

                // String resultText = String.valueOf(idx + 1) + ":" + (String)spinner.getSelectedItem(); <-修正しました↓
                String resultText = String.valueOf((idx + 1) + ":" + (String)spinner.getSelectedItem());

                // 選択されているアイテムを取得
                resultTextView.setText(resultText);
                Log.d("selected", "result" + (String)spinner.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 選択されたなかった時の処理
            }
        });
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="370dp"
        android:layout_height="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:entries="@array/work"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:text="TextView"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner" />

</android.support.constraint.ConstraintLayout>

strings.xml
<resources>
    <string name="app_name">spinner</string>
    <string-array name="work">
        <item>大工</item>
        <item>配管工</item>
        <item>土木</item>
        <item>建築士</item>
        <item>電気工事</item>
    </string-array>
</resources>

좋은 웹페이지 즐겨찾기