Androidアプリケーション開発UI(ドロップダウンリスト)


Androidプラットフォームでドロップダウンリストを使用したSpinnerコンポーネントの効果図は以下の通りです.
Android应用开发UI(下拉列表)_第1张图片  
コード:
1,res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title_tx">      :       :</string>
    <string name="app_name">    (Spinner)</string>
    <string name="about_80080088">    ,   http://www.80080088.com  。</string>
</resources>

 
2,res/layout/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:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/title_tx"
	    android:id="@+id/title_tx"/>
	<TextView 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:text="@string/about_80080088"
		android:id="@+id/about_80080088">
	</TextView>
	<Spinner 
		android:layout_width="match_parent" 
		android:layout_height="wrap_content" 
		android:id="@+id/spinnerAge">
	</Spinner>
</LinearLayout>

 
3.Activityクラス
package com.cottsoft.android;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class A008Activity extends Activity {
	private static final String[] m_Ages = {"0 18 ","19 30 ","31 60 ","61  80 ","81   "};
	
	private TextView m_title_tx,m_about_80080088;
	private Spinner m_Spinner;
	
	private ArrayAdapter<String> adapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_title_tx = (TextView) findViewById(R.id.title_tx);
        m_about_80080088=(TextView) findViewById(R.id.about_80080088);
        m_Spinner = (Spinner) findViewById(R.id.spinnerAge);
        
        m_title_tx.setTextSize(22);
        m_about_80080088.setTextSize(15);
        m_about_80080088.setTextColor(Color.GRAY);
        
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,m_Ages);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        m_Spinner.setAdapter(adapter);
        
        m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				m_title_tx.setText(getResources().getString(R.string.title_tx)+m_Ages[arg2]);
				arg0.setVisibility(View.VISIBLE);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
        	
        });
    }
}