1.strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">S02_E01_SpinnerEtc</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string> <string-array name="planets">
<item>地球</item>
<item>火星</item>
<item>水星</item>
<item>木星</item>
</string-array>
</resources>

2.activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.s02_e01_spinneretc.MainActivity" > <Spinner
android:id="@+id/mSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </RelativeLayout>

3.item.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/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

4.java

 package com.example.s02_e01_spinneretc;

 import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner; @SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{ private Spinner mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.mSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSpinner.setAdapter(adapter);
mSpinner.setPrompt("测试");
mSpinner.setOnItemSelectedListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
System.out.println(parent.getItemAtPosition(position)); } @Override
public void onNothingSelected(AdapterView<?> parent) {
System.out.println(parent); }
}

5.自定义下拉源

 package mars.spinner;

 import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity {
/** Called when the activity is first created. */
//声明Spinner对象
private Spinner spinner = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过ID,得到Spinner对象
spinner = (Spinner) findViewById(R.id.spinnerId);
List<String> list = new ArrayList<String>();
list.add("test1");
list.add("test2");
//调用ArrayAdapter的构造函数来创建ArrayAdapter对象
//第一个参数是指上下文对象
//第二个参数指定了下拉菜单当中每一个条目的样式
//第三个参数指定了TextView控件的ID
//第四个参数为整个列表提供数据
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.textViewId,list);
//通过createFromResource方法创建一个ArrayAdapter对象
//第一个参数是指上下文对象
//第二参数引用了在strings.xml文件当中定义的String数组
//第三个参数是用来指定Spinner的样式,是一个布局文件ID,该布局文件由Android系统提供,也可替换为自己定义的布局文件
/*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array,
android.R.layout.simple_spinner_item);
*/ //设置Spinner当中每个条目的样式,同样是引用一个Android系统提供的布局文件
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter);
spinner.setPrompt("测试");
//为spinner对象绑定监听器
spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener());
}
//这个监听器主要用来监听用户选择列表的动作
class SpinnerOnSelectedListener implements OnItemSelectedListener{ //当用户选定了一个条目时,就会调用该方法
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position,
long id) {
String selected = adapterView.getItemAtPosition(position).toString();
System.out.println(selected);
} @Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
System.out.println("nothingSelected");
} }
}

ANDROID_MARS学习笔记_S02_001_Spinner的更多相关文章

  1. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  2. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. ANDROID_MARS学习笔记_S01_011ProgressBar

    文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...

  4. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  6. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  7. ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

    一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  8. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  9. ANDROID_MARS学习笔记_S01_005CheckBox

    一. 1.checkbox_layout.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...

随机推荐

  1. 使用netbeans 搭建 JSF+SPRING 框架

    spring版本使用4,jsf版本2.2 jsf的配置文件faces-config.xml <?xml version='1.0' encoding='UTF-8'?> <faces ...

  2. Spring切面处理

    切面类DictionaryAOP 里面有一个全局变量systemTestDomain <bean id="dictionaryAOP" class="test.Di ...

  3. (转)unity开发相关环境(vs、MonoDevelop)windows平台编码问题

    转自: http://www.cnblogs.com/sevenyuan/archive/2012/12/06/2805114.html 1.unity会爆出错误: There are inconsi ...

  4. vue防止闪烁

    v-text也可以 转意的话使用v-html <style> [v-clock]{ display:none } <style> <span>{{msg}}< ...

  5. javascript 正则 验证 第25节

    <html> <head> <title>Form对象</title> <script type="text/javascript&qu ...

  6. android 权限总结

    1.拨打电话要权限 2.sd目录存东西要权限

  7. Java多线程间通信-解决安全问题、等待唤醒机制

    /*1.增加一个知识点一个类怎么在所有的类中,让其它类来共同修改它的数据呢?可以用单例设计模式可以用静态可以在其它类中做一个构造函数,接受同一个对象,这样就可以实现对象 2.状态选择可以用数字0 1 ...

  8. Mac:How to mount a Windows shared folder

    Reference: How to mount a Windows shared folder on your Mac

  9. Qt-获取主机网络信息之QHostInfo

    Qt中提供了几个用于获取主机网络信息的类,包括QHostInfo.QHostAddress.QNetworkInterface以及QNetworkAddress.在本节中,我将在这里总结QHostIn ...

  10. 排序算法ONE:选择排序SelectSort

    /** *选择排序: * 对冒泡排序的一个改进 * 进行一趟排序时,不用每一次都交换,只需要把最大的标示记下 * 然后再进行一次交换 */ public class SelectSort { /** ...