如果你是只能手机,大家可以来这里先来了解以下Android系统,作为参考。
- //SpinnerDemo.java:
- package com.cci.SpinnerDemo;
- 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 SpinnerDemo extends Activity {
- TextView tv;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv=(TextView)findViewById(R.id.mytxt);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- this,android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(
- android.R.layout.simple_spinner_dropdown_item);
- //随便写什么东西
- adapter.add("red");
- adapter.add("green");
- adapter.add("blue");
- Spinner spinner = (Spinner) findViewById(R.id.myspinner);
- spinner.setAdapter(adapter);
- spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> parent,
- View view,int position,long id) {
- Spinner spinner = (Spinner) parent;
- tv.setText(spinner.getSelectedItem().toString());
- //Log.v("Test", "id = " + id + "("
- //+ spinner.getSelectedItem().toString() + ")");
- }
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- }
- <!--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:id="@+id/mytxt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Spinner
- android:id="@+id/myspinner"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- ></Spinner>
- </LinearLayout>
【编辑推荐】