Android應用開發學習之列表選擇框

來源:互聯網
上載者:User

 

本文我們來看列表選擇框的實現。程式運行效果如所示:

 

主布局檔案main.xml內容如下所示:

 

<PRE class=html name="code"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/prompt" />    <Spinner     android:entries="@array/brands"    android:layout_height="wrap_content"    android:layout_width="wrap_content"     android:id="@+id/spinner"/><Button android:text="提交"     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>  </LinearLayout></PRE><BR><PRE></PRE><P>Spinner標記指定了列表選擇框,列表選擇框的內容是由android:entries屬性指定的,即數組資源@array/brands,我們將該數組定義在一個單獨的資源檔BrandArray.xml中,其定義如下:</P><PRE class=html name="code"><?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="brands">    <item>蘋果</item>    <item>三星</item>    <item>HTC</item>    <item>諾基亞</item>    <item>聯想</item>    <item>華為</item>    <item>其它</item>    </string-array></resources></PRE><P>&nbsp;</P><P><SPAN style="FONT-SIZE: 14px">最後,編寫主<SPAN style="FONT-FAMILY: Calibri">Activity</SPAN>檔案,響應使用者的操作,其內容如下:</SPAN><BR></P><P><SPAN style="FONT-SIZE: 14px"></SPAN>&nbsp;</P><SPAN style="COLOR: teal"></SPAN><PRE class=html name="code">package com.liuhaoyu;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.Button;import android.widget.Spinner;import android.widget.Toast;import android.widget.AdapterView.OnItemSelectedListener;public class MainActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final Spinner spinner = (Spinner) findViewById(R.id.spinner);                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {        @Overridepublic void onItemSelected(AdapterView<?> parent, View arg1,int pos, long id) {String result = parent.getItemAtPosition(pos).toString();Log.i("選擇結果是:", result);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});                Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"您選擇的品牌是:" + spinner.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();}});    }}</PRE><P align=left><BR><SPAN style="COLOR: black">&nbsp;</SPAN></P><P><SPAN style="COLOR: black">以上我們是通過在布局檔案中通過數組資源直接為列表選擇框指定列表內容,如果不在布局檔案中指定,也可以通過使用適配器的方式為其指定列表內容。下面我們來看一個例子,其運行效果如下:</SPAN></P><P><IMG alt="" src="http://img.blog.csdn.net/20130801163255843?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1aGFveXV0eg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast"></P><P><SPAN style="FONT-SIZE: 14px">主布局檔案<SPAN style="FONT-FAMILY: Calibri">main.xml</SPAN>內容如下:</SPAN></P><SPAN style="COLOR: teal"></SPAN><PRE class=html name="code"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/prompt" />    <Spinner     android:layout_height="wrap_content"    android:layout_width="wrap_content"     android:id="@+id/spinner"/>    <Button android:text="提交"     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>  </LinearLayout></PRE><P align=left><BR><SPAN style="COLOR: teal">主</SPAN><SPAN style="COLOR: teal">Activity</SPAN><SPAN style="COLOR: teal">檔案內容如下:</SPAN></P><SPAN style="FONT-FAMILY: Calibri; FONT-SIZE: 14px"></SPAN><PRE class=java name="code"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/prompt" />    <Spinner     android:layout_height="wrap_content"    android:layout_width="wrap_content"     android:id="@+id/spinner"/>    <Button android:text="提交"     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>  </LinearLayout>主Activity檔案內容如下:package com.liuhaoyu;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final Spinner spinner = (Spinner) findViewById(R.id.spinner);                // 通過在代碼中定義數組來建立適配器.        String[] brand=new String[]{"蘋果","三星","HTC","諾基亞","聯想","華為","其它"};ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,brand); // 也可以使用數組資源來建立適配器,而不是象上面那樣在代碼中定義 數組。 //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(//this, R.array.brands,android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(adapter); // 將適配器與挑選清單框關聯                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {        @Overridepublic void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id) {String result = parent.getItemAtPosition(pos).toString();Log.i("選擇結果是:", result);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});                Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"您選擇的品牌是:" + spinner.getSelectedItem().toString(),Toast.LENGTH_SHORT).show();}});    }    }</PRE><P>&nbsp;</P>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.