Android[初級教程]第四篇 Spinner控制項

來源:互聯網
上載者:User

還是接上一篇,這次妖精們說了,不要用RadioButton控制項了,不好看,還佔地方,抓一個人,要佔四個地方,那我抓一個人只要佔一個地方就行了,於是用了Spinner控制項,還是一次抓一個

main.xml如下

view plain
</pre><pre class="html" name="code"><?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"> 
    <Spinner android:layout_height="wrap_content" android:id="@+id/spinner" 
        android:layout_width="wrap_content"></Spinner> 
    <TextView android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:text="@string/hello" 
        android:id="@+id/text"></TextView> 
</LinearLayout> 

Activity的java代碼如下:

view plain
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 ButtonDemoActivity extends Activity implements OnItemSelectedListener 

    private TextView text = null; 
    private Spinner spinner; 
    private String[] item = { "唐僧", "孫悟空 ", "豬八戒", "沙和尚" }; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        // 通過ID尋找到main.xml中的TextView控制項 
        text = (TextView) findViewById(R.id.text); 
 
        // 通過ID尋找到main.xml中的Spinner控制項 
        spinner = (Spinner) findViewById(R.id.spinner); 
         
        //設定一個Array適配器,將數組資料放入適配器中 
        ArrayAdapter adapter = new ArrayAdapter(this, 
                android.R.layout.simple_spinner_item, item); 
        //設定下拉式清單的樣式 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
         
        //對Spinner進行適配 
        spinner.setAdapter(adapter); 
         
        //Spinner中事件選擇的監聽 
        spinner.setOnItemSelectedListener(this); 
 
    } 
 
    private void updateText(String string) 
    { 
        // 將文本資訊設定給TextView控制項顯示出來 
        text.setText(string); 
    } 
 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, 
            long arg3) 
    { 
        String str = "這次妖精把" + item[position] + "抓住了!"; 
        updateText(str); 
         
    } 
 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) 
    { 
        // TODO Auto-generated method stub 
         
    } 
 

你要說妖精們的事還真多,一會這樣,一會那樣的,用我們現在的話來說就叫"眾口難調"啊,畢竟妖精多了去了,


 

摘自:kangkangz4的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.