“ 매주 목요일마다 당신이 항상 하던대로 신발끈을 묶으면 신발이 폭발한다고 생각해보라.
컴퓨터를 사용할 때는 이런 일이 항상 일어나는데도 아무도 불평할 생각을 안 한다. ”- Jef Raskin
맥의 아버지 - 애플컴퓨터의 매킨토시 프로젝트를 주도
반응형
안드로이드스튜디오,스피너,spinner,과제,앱,어플,모바일,아이폰,대학생활,코딩일기
스피너를 활용하여 만들어보자!
과제 일시 : 2020 - 05 - 14
오늘은 저번과 비슷하게 Spinner를 활용하여, 앱을 만들려고 합니다. Strings.xml에 먼저 아이템을 미리 만들어 놓고 어플을 실행하면 spinner를 선택하면 해당 선택한 아이템을 토스트 메세지로 출력하여 알려줄 수 있게 했습니다.
<실행화면>
<소스 코드>
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.21" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.21" />
<Button
android:id="@+id/btn_refresh"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.21"
android:text="선정" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
* MainActivity.java
package com.example.spinneraddr;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ArrayAdapter<CharSequence> adapterCity, adapterGu;
String choiceCity;
String choiceGu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinnerCity = (Spinner) findViewById(R.id.spinner);
final Spinner spinnerGu = (Spinner) findViewById(R.id.spinner2);
final Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
adapterCity = ArrayAdapter.createFromResource(this, R.array.spinner_city, android.R.layout.simple_dropdown_item_1line);
adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapterCity);
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (adapterCity.getItem(position).equals("1반")) {
choiceCity = "1반";
adapterGu = ArrayAdapter.createFromResource(MainActivity.this, R.array.spinner_gu_1ban, android.R.layout.simple_dropdown_item_1line);
adapterGu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGu.setAdapter(adapterGu);
spinnerGu.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
choiceGu = adapterGu.getItem(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} else if (adapterCity.getItem(position).equals("2반")) {
choiceCity = "2반";
adapterGu = ArrayAdapter.createFromResource(MainActivity.this, R.array.spinner_gu_2ban, android.R.layout.simple_dropdown_item_1line);
adapterGu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGu.setAdapter(adapterGu);
spinnerGu.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
choiceGu = adapterGu.getItem(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btn_refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "선정 된 학생은 " + choiceCity + "소속이고, " + choiceGu + "입니다", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
* strings.xml
<resources>
<string name="app_name">SpinnerAddr</string>
<string-array name="spinner_city">
<item>1반</item>
<item>2반</item>
</string-array>
<string-array name="spinner_gu_1ban">
<item>김구구</item>
<item>김동동</item>
<item>박응아</item>
<item>전기긱</item>
<item>주정차</item>
<item>최강창민</item>
</string-array>
<string-array name="spinner_gu_2ban">
<item>장종찬</item>
<item>최영환</item>
<item>권순성</item>
<item>김치밥</item>
<item>김붕붕</item>
<item>페라리</item>
</string-array>
</resources>
Okay ! 좋아 ! 실행이 잘된다 :D