Spinner的使用方法
Spinner_demo.xml這個布局文件的寫法如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical" >
?
??? <Spinner
??????? android:id="@+id/spinner1"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content" />
?
??? <TextView
??????? android:id="@+id/textView1"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="被選中的人名顯示在下面"
??????? android:textSize="25sp"
??????? />
?
</LinearLayout>
?
ASpinner
package com.example.uidemo3;
?
import java.util.ArrayList;
?
import android.R.anim;
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 ASpinner extends Activity {
?
?????? Spinner spinner1;
?????? ArrayList<String> arrayList = new ArrayList<String>();
?
?????? TextView textView1;
??????
?????? OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {
?
????????????? /*
????????????? ?* parent The AdapterView where the selection happened
????????????? ?*view The view within the AdapterView that was clicked
???????? *position The position of the view in the adapter
???????? *id The row id of the item that is selected
???????? *
???????? *AdapterView<?>代表Spinner
???????? *View代表Spinner里面的item
????????????? ?*/
????????????? @Override
????????????? public void onItemSelected(AdapterView<?> parent, View view,
??????????????????????????? int position, long id) {
????????????????????
???????????????????? textView1.setText( ((TextView)view).getText());
????????????? }
?
????????????? @Override
????????????? public void onNothingSelected(AdapterView<?> parent) {
????????????? }
?????? };
?
?????? @Override
?????? protected void onCreate(Bundle savedInstanceState) {
????????????? super.onCreate(savedInstanceState);
????????????? this.setContentView(R.layout.spinner_demo);
?
????????????? spinner1 = (Spinner) this.findViewById(R.id.spinner1);
?
????????????? textView1 = (TextView) this.findViewById(R.id.textView1);
?
????????????? arrayList.add("小倉優子");
????????????? arrayList.add("蒼井空");
????????????? arrayList.add("松島楓");
????????????? arrayList.add("波多野結衣");
????????????? arrayList.add("櫻田櫻");
????????????? arrayList.add("小峰由衣");
????????????? arrayList.add("早乙女");
????????????? arrayList.add("小泉彩");
?
????????????? // 適配的概念Adapter,主要負責適配數據和各種樣式
????????????? // 下拉菜單的樣式
????????????? @SuppressWarnings({ "unchecked", "rawtypes" })
????????????? ArrayAdapter arrayAdapter = new ArrayAdapter(this,
??????????????????????????? android.R.layout.simple_spinner_item, arrayList);
????????????? // 以下顯示的是一行,只有一行能看的見
????????????? // arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
?
????????????? // 這種是帶有文本和radio的下拉效果。
????????????? arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
?
????????????? // arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
?
????????????? // 為下拉的spinner設置內容
????????????? spinner1.setAdapter(arrayAdapter);
????????????? //為spinner1設置監聽事件
????????????? spinner1.setOnItemSelectedListener(onItemSelectedListener);
?????? }
}
?
總結
以上是生活随笔為你收集整理的Spinner的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奔驰c级发布会结束,带你了解新款奔驰c级
- 下一篇: 纯visa卡在国内怎么办