●스피너(spinner)

- html의 select와 비슷한 것이다. 여러개 중 하나를 선택 할수 있게 해준다.


- ListView와 마찬가지로 adapter 객체를 이용하여 보여준다.





●스피너 예제1


- ex1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >


    <Spinner

        android:id="@+id/spinner"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#aec7e1" />


</LinearLayout>




- ex1.java

package com.example.day0314;


import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.Spinner;


public class Ex1 extends Activity{

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.ex1);

//스피너

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

//어댑터 생성

            //이 예제 같은 경우 string,xml에 리스트를 추가해 놓고 그 리스트를 불러온다.

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.number, android.R.layout.simple_spinner_item);

//스피너와 어댑터 연결

spinner.setAdapter(adapter);

}

}




- string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string name="app_name">day0314</string>

    <string name="hello_world">Hello world!</string>

    

    <string-array name="number">

        <item >1번</item>

        <item >2번</item>

        <item >3번</item>

        <item >4번</item>

        <item >5번</item>

    </string-array>

</resources>

 








●스피너 예제2

- 스피너를 선택하면 이미지보여주는 액티비티 실행


- ex2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    

    

    <Spinner 

        android:id="@+id/spinner"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#aec7e1"/>

    

</LinearLayout>




- Ex2.java

package com.example.day0314;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;


public class Ex2 extends Activity implements OnItemSelectedListener{

int img[] = {

0,

R.drawable.img0,

R.drawable.img1,

R.drawable.img2,

R.drawable.img3,

R.drawable.img4,

R.drawable.img5,

R.drawable.img6,

};

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.ex2);

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

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.imgName, android.R.layout.simple_spinner_item);

spinner.setAdapter(adapter);

//이벤트를 일으킨 위젯과 리스너와 연결

spinner.setOnItemSelectedListener(this);

}

public void showDetail(int position){

Intent intent = new Intent(this, PhotoActivity.class);

intent.putExtra("data", img[position]);

startActivity(intent);

}

//콜백메서드:이벤트 등이 발생하면 개발자가 호출하는 것이 아니라 시스템에 의해 호출되는 메서드이다.

//아이템 중 하나를 선택 했을때 호출되는 콜백 메서드

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

if(position!=0){

Toast.makeText(this, "이미지"+(position)+" 클릭되었습니다", Toast.LENGTH_SHORT).show();

showDetail(position);

}

}


@Override

public void onNothingSelected(AdapterView<?> parent) {

}

}




- photo.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ImageView 

        android:id="@+id/imgView_photo"

        android:layout_width="wrap_content"

        android:layout_height="300dp"

        android:layout_gravity="center_horizontal"

        android:src="@drawable/img0"

        android:layout_marginLeft="20dp"

        android:layout_marginRight="20dp"/>

     <Button 

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/btn_close"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="닫기"

        android:layout_gravity="center_horizontal"/>

     

    

</LinearLayout>

 



- PhotoActivity.java

package com.example.day0314;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;


public class PhotoActivity extends Activity implements OnClickListener{

//현재 이미지 자원나타내는 변수

int current_img;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.photo);

Button btn_close = (Button)findViewById(R.id.btn_close);

btn_close.setOnClickListener(this);

//이미지가 교체

ImageView imgView_photo = (ImageView)findViewById(R.id.imgView_photo);

Intent intent = this.getIntent();

current_img = intent.getIntExtra("data", 0);

imgView_photo.setImageResource(current_img);

}


@Override

public void onClick(View v) {

finish();

}

}

 



- string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string name="app_name">day0314</string>

    <string name="hello_world">Hello world!</string>

    

    <string-array name="imgName">

    <item >▼선택하세요</item>

    <item >이미지1</item>

   <item >이미지2</item>

   <item >이미지3</item>

   <item >이미지4</item>

   <item >이미지5</item>

   <item >이미지6</item>

   <item >이미지7</item>

    </string-array>


</resources>













●액티비티에서 클릭 이벤트가 있으면 OnClickListener를 구현하고, 선택 이벤트가 있으면 OnItemSelectedListener를 구현한다.















Posted by -현's-

댓글을 달아 주세요