ANDROID_MARS学习笔记_S02_001_Spinner

时间:2023-11-09 23:40:26

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");
} }
}