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


  對於手機和平板電腦的應用程序來說,打字是非常不方便的操作方式,比較好的方式就是列出一組選項讓用戶挑選,這樣就可以避免打字的麻煩。使用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);
}
};
}
效果截圖:
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM