spinner的兩種創建方式

                       spinner的兩種創建方式

spinner是安卓中一個常見的控件,它的中文含義是下拉列表的意思,它有兩種創建方式:

第一種:在佈局中設置

1,在res文件夾下的values中的strings.xml中添加一個string類型的數組,形式如下:

<span style="font-family:KaiTi_GB2312;font-size:24px;"><resources>

    <string name="app_name">spinner</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="cities">
        <item>北京</item>
        <item>南京</item>
        <item>上海</item>
        <item>深圳</item>
        <item>浙江</item>
        <item>天津</item>
    </string-array>

</resources></span>
2,在佈局xml文件中添加spinner控件

<span style="font-family:KaiTi_GB2312;font-size:24px;"><LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="請選擇您所在的城市"
       android:textSize="20sp"
        />

    <Spinner
        android:id="@+id/spinner"
</span><pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:24px;">        android:entries="@array/cities"</span>
  android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>

3,java代碼

<span style="font-family:KaiTi_GB2312;font-size:24px;">Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = ArrayAdapter
	.createFromResource(this, R.array.photo_from, 
	android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.
	R.layout.simple_spinner_dropdown_item);
</span><pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:24px;">spinner .setAdapter(adapter);</span>




第二種:在java代碼中動態添加

1,在佈局中添加一個textView和spinner控件

<span style="font-family:KaiTi_GB2312;font-size:24px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout></span>

2,在java代碼中實現邏輯

<span style="font-family:KaiTi_GB2312;font-size:24px;">public class MainActivity extends Activity {

	private static final String cities[]={"北京","上海","天津","廣州","深圳"};
	private TextView textview;
	private Spinner spinner;
	private ArrayAdapter<String> adapter; 
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			textview=(TextView)findViewById(R.id.textView);
			spinner=(Spinner)findViewById(R.id.spinner);
			adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cities);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			spinner.setOnItemSelectedListener(new OnItemSelectedListener()
			{

				@Override
				public void onItemSelected(AdapterView<?> parent, View v,
						int position, long id) {
					// TODO Auto-generated method stub
					textview.setText("您選擇的城市是:"+cities[position]);
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
				
			});
			
		}

}
</span>

注意:這裏的
<span style="font-family:KaiTi_GB2312;font-size:24px;"> android.R.layout.simple_spinner_item</span>
<span style="font-family:KaiTi_GB2312;font-size:24px;">android.R.layout.simple_spinner_dropdown_item</span>
是系統封裝好的佈局,不用自己添加,所以前綴必須加android


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章