W.A 저장소

Android Spinner 본문

Programing

Android Spinner

W.A 2010. 7. 20. 14:39
package com.karyu.androidstudy;

import android.app.Activity;
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.TextView;

public class AndroidStudyActivity extends Activity {
TextView selection;
String[] items = { "lorem", "ipsum", "dolor", "sit" };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        selection = (TextView)findViewById(R.id.selection);
        
        Spinner spin = (Spinner)findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(new SpinnerListen());
        
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
         android.R.layout.simple_spinner_item, items);
        
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }
    
    private class SpinnerListen implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selection.setText(items[arg2]);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
selection.setText("");
}
    }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView 
android:text="@+id/TextView01" 
android:id="@+id/selection" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</TextView>

<Spinner 
android:id="@+id/spinner" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:drawSelectorOnTop="true">
</Spinner>

</LinearLayout>

'Programing' 카테고리의 다른 글

Layout 예제 1.  (0) 2010.08.04
Android Database(Sqlite) Instruction  (2) 2010.07.29
Android CheckBox  (0) 2010.07.20
Programming Reference  (0) 2010.07.16
Screen Capture Class(화면 캡쳐 클래스) Robot  (0) 2010.07.16