SoFunction
Updated on 2025-04-06

A09_Spinner (drop-down list) custom settings


package .a09_spinner;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
private Spinner spinner;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
spinner = (Spinner)findViewById();
textView = (TextView)findViewById();
//Create an ArrayAdapter
// Static use of xml file to set the drop-down list content
/**
* ArrayAdapter parameter description:
* First: Context object
* The second: the id of the data source of the drop-down menu
* The third: The style of the drop-down menu, which uses the style of the Android standard drop-down menu.
*/
//ArrayAdapter<CharSequence> adapter = (this, , .simple_spinner_item);
//Call the setDropDownViewResource() method to set the style of each option in the drop-down list. Here, the Android standard style is also used.
//(.simple_spinner_dropdown_item);
//Dynamic settings of drop-down list content
List<String> list = new ArrayList<String>();
("yesterday");
("today");
("tomorrow");
/**
* Parameters
* First: Context object
* The second: Customize the style of the options for the drop-down menu
* Third: Customize the style id of the drop-down menu option control
* The fourth: List data
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, ,.list_textViewId,list);
//Add an adapter for spinner
(adapter);
//Set the title of the Spinner drop-down list...
("These three days are only");
//Bind the listener for spinner
(new SpinnerListener());
}
//This listener is used to monitor the operation of multiple spinners of users
class SpinnerListener implements OnItemSelectedListener{
//This method will be called when the user selects the option in the first pull list
/**
* Parameter description:
*First: The current drop-down list, that is, the parent view of the third parameter
*Second: currently selected option
*Third: Location of the selected option
*Fourth: id of the selected option
*/
public void onItemSelected(AdapterView<?> adapterView, View view, int position,
long id) {
//Get the options selected by the user
String selected = "Your choice is:"+(position).toString();
(selected);
(, selected, Toast.LENGTH_SHORT).show();
}
//This method is called when the user does not make a choice
public void onNothingSelected(AdapterView<?> arg0) {
(, "You did not select any options", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(.activity_main, menu);
return true;
}
}