For mobile phones and tablet applications, typing is a very inconvenient way to operate, and a better way to do this is to list a set of options for the user to choose from, thus avoiding the hassle of typing. Use the Spinner drop-down menu component to complete the following steps:
1. Create a list of options, with many project names in the options list, which are represented by arrays;
2. Set the option list to a spinner interface component;
3. Set the menu display format of the spinner component;
4. Set the Onitemselectedlistener () event handler for the spinner component, and when the user clicks on a project, the program must get the data for that item.
Special note: There are two ways to create a list of options, the first of which is to declare the list of options directly in the program as an array. This method is relatively simple, but we mentioned in the fifth chapter of the MVC design pattern, it is mentioned that should try to separate the program code and text data, so there is a second option list to establish the way. We set up the project list in the project's Strings.xml file, allowing the program to get an array of option lists from the project's resource class R.
We can define a menu format definition file ourselves:
<?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 file:
<?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 file:
<resources>
<string name= "app_name" > Fitness Consulting </string>
<string name= "promptsex" > Sex:</string>
<string name= "spnsexprompt" > Sex:</string>
<string name= "Promptage" > Age:</string>
<string name= "Promptbtndosug" > Fitness Consulting </string>
<string name= "Sugresult" > Results:</string>
<string name= "Sugrun" > Running </string>
<string name= "Sugswim" > Swimming </string>
<string name= "sugsuggestion" > Health Consulting </string>
<string name= "Sexmale" > Men </string>
<string-array name= "Spnsexlist" >
<item> men </item>
<item> Women </item>
</string-array>
</resources>
Program code:
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 ()
{
//Get interface components from resource class 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);
Listener of//button component events
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 = "Result:";
if (ssex.equals ("male"))
{
if (Iage <)
Strsug + = getString (r.string.sugrun);
else if (iage >)
Strsug + = getString (r.string.sugrun);
Else
Strsug + = getString (r.string.sugrun);
}
Else
{
if (Iage <)
Strsug + = getString (r.string.sugrun);
else if (iage >)
Strsug + = getString (r.string.sugswim);
Else
Strsug + = getString (r.string.sugswim);
}
Txtresult.settext (STRSUG);
}
};
}
Effect:
Getting Started with Android (vii): Spinner drop-down menu component