Android入門(七):Spinner下拉式菜單組件

來源:互聯網
上載者:User

標籤:

  對於手機和平板電腦的應用程式來說,打字是非常不方便的操作方式,比較好的方式就是列出一組選項讓使用者挑選,這樣就可以避免打字的麻煩。使用Spinner下拉式功能表組件需要完成以下幾個步驟:

  1.建立選項列表,選項列表中包含許多項目名稱,這些項目名稱是用數組的方式代表;

  2.把選項列表設定給一個Spinner介面組件;

  3.設定Spinner組件的菜單顯示格式;

  4.設定Spinner組件的OnItemSelectedListener()事件處理常式,當使用者單擊某個項目之後,程式必須取得該項目所對應的資料。

  特別提示:建立選項列表有兩種方式,第一種是直接將選項列表以數組的方式宣告在程式中。這種方式比較簡單,但是我們在第五章提到過MVC設計模式,裡面提到過應該盡量將程式碼與文字等資料分開,所以就有了第二種選項列表建立方式。我們把項目列表建立在項目的strings.xml檔案中,在讓程式從項目的資源類R中取得選項列表數組。

 

  我們可以自己定義一個菜單格式定義檔案:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>

  main.xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">

<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptSex"/>
<Spinner
android:id="@+id/spnSex"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:drawSelectorOnTop="true"
android:prompt="@string/spnSexPrompt"/>
android:spinnerMode="dialog"/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptAge"/>
<EditText
android:id="@+id/edtAge"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="number"
android:text=""/>
<Button
android:id="@+id/btnDoSug"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptBtnDoSug"/>
<TextView
android:id="@+id/txtResult"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/sugResult"/>
</LinearLayout>

strings.xml檔案:
<resources>
<string name="app_name">健身諮詢</string>
<string name="promptSex">性別:</string>
<string name="spnSexPrompt">性別:</string>
<string name="promptAge">年齡:</string>
<string name="promptBtnDoSug">健身諮詢</string>
<string name="sugResult">結果:</string>
<string name="sugRun">跑步</string>
<string name="sugSwim">遊泳</string>
<string name="sugSuggestion">健康諮詢</string>
<string name="sexMale">男</string>
<string-array name="spnSexList">
<item>男</item>
<item>女</item>
</string-array>
</resources>

程式碼:
public class MainActivity extends Activity
{

private Button btnDoSug;
private EditText edtAge;
private TextView txtResult;
private Spinner spnSex;

private String sSex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViewComponent();
}

private void setupViewComponent()
{
//從資源類R中取得介面組件
btnDoSug = (Button)findViewById(R.id.btnDoSug);
spnSex = (Spinner)findViewById(R.id.spnSex);
edtAge = (EditText)findViewById(R.id.edtAge);
txtResult = (TextView)findViewById(R.id.txtResult);

ArrayAdapter<CharSequence> adapSexList = ArrayAdapter.createFromResource(
this, R.array.spnSexList, R.layout.spinner_layout);
spnSex.setAdapter(adapSexList);
spnSex.setOnItemSelectedListener(spnSexItemSelLis);

//button組件事件的listener
btnDoSug.setOnClickListener(btnDoSugOnClick);
}

private Spinner.OnItemSelectedListener spnSexItemSelLis = new Spinner.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parent, View v, int position, long id)
{
sSex = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView parent)
{
//null
}
};

private Button.OnClickListener btnDoSugOnClick = new Button.OnClickListener()
{

public void onClick(View view){

int iAge = Integer.parseInt(edtAge.getText().toString());

String strSug = "結果:";
if(sSex.equals("男"))
{
if(iAge < 28)
strSug += getString(R.string.sugRun);
else if(iAge > 33)
strSug += getString(R.string.sugRun);
else
strSug += getString(R.string.sugRun);
}
else
{
if(iAge < 28)
strSug += getString(R.string.sugRun);
else if(iAge > 33)
strSug += getString(R.string.sugSwim);
else
strSug += getString(R.string.sugSwim);
}

txtResult.setText(strSug);
}
};
}
效果:
 

Android入門(七):Spinner下拉式菜單組件

聯繫我們

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