本文学习{android自学指南} 自己写了一遍,并且理解了
1、建一个新的项目,打开res/values/strings.xml文件,写入如下代码:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, Android Demo!</string>
- <string name="MyPhone">MyPhone YC</string>
- <string name="MyMessage">MyMessage Yc</string>
- <string name ="spinner">SpinnerActivity</string>
- <string name="color">请选择一个颜色 :</string>
- <string-array name="colors">
- <item>黑色 | Black</item>
- <item>蓝色 | Blue</item>
- <item>棕色 | Brown</item>
- <item>绿色 | Green</item>
- <item>红色 | Red</item>
- <item>灰色 | Grey</item>
- <item>粉色 | Pink</item>
- <item>紫色 | Purple</item>
- <item>白色 | White</item>
- <item>黄色 | Yellow</item>
- </string-array>
- </resources>
2.打开res/layout/spinner.xml 写入如下内容:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android=""
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <TextView android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:text="@string/color"
- android:layout_marginTop="10dip" />
- <Spinner android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:id="@+id/spinner" />
- </LinearLayout>
3.打开SpinnerActivity.java 写入下内容:
- package cn.com.android;
- 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.Toast;
- public class SpinnerActivity extends Activity {
- private Spinner spinner;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 1.通过ID得到XML中定义的下拉列表
- spinner = (Spinner) findViewById(R.id.spinner);
- // 2.准备数组适配器
- ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
- R.array.colors, android.R.layout.simple_spinner_item);
- // 设置下拉样式
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
- // 为下拉列表设置适配器
- spinner.setAdapter(adapter);
- // 3.定义子元素选择监听器
- OnItemSelectedListener oisl = new OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- Toast.makeText(
- SpinnerActivity.this,
- "选择的色彩:"
- + parent.getItemAtPosition(position).toString(),
- Toast.LENGTH_LONG).show();
- }
- public void onNothingSelected(AdapterView<?> parent) {
- }
- };
- //为下拉列表绑定事件
- spinner.setOnItemSelectedListener(oisl);
- }
- }
4.在AndroidManifest.xml中注册
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android=""
- package="cn.com.demo"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="11" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SpinnerActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
5.运行程序:
阅读(1531) | 评论(0) | 转发(0) |