How to use the Spinner control in Android Studio 2-1

The function of the Spinner control is to display all sub-items at the same time, and the user can select the displayed sub-items, as shown in Figure 1.

Figure 1 Spinner control

1 Creation of the Spinner control

Create a Spinner control through the layout XML file, the code is shown in Figure 2.

Figure 2 Create Spinner via XML

2 Use of the Spinner control

2.1 Associated controls

In the main program, first create an object of the Spinner class, and then associate the object with the Spinner control created in "1 Creation of the Spinner Control". The code is as follows.

Figure 3 Variables are associated with controls

Among them, sp1 is a member variable of the MainActivity class, which is an object of the Spinner class (Figure 3①); then define the member function initWidget() of the MainActivity class, and associate sp1 with the Spinner control in this function (Figure 3②); Call initWidget() in the onCreate() function of the MainActivity class (Figure 3③).

2.2 Set the sub-items of the Spinner control

SpinnerAdapter is used to represent the data of Spinner control subitem. SpinnerAdapter is an interface derived from Adapter, which is a bridge between the Spinner control and its child item data. So next, first associate the subitem data with the SpinnerAdapter, and then associate the SpinnerAdapter with the Spinner control, so that you can set the subitem data of the Spinner control.

2.2.1 Associating data with SpinnerAdapter

Use the code shown in Figure 4 to associate data with the SpinnerAdapter.
 
 
Figure 4 Setting SpinnerAdapter
Among them, the string array array_colors is defined, which stores the data displayed in the Spinner control (Figure 4①), but the string array cannot be directly used in the Spinner class, and SpinnerAdaper is needed as an intermediary, so, in Figure 4① Defines the object adapter of the SpinnerAdaper class. After that, configure the adapter object through the constructor of the ArrayAdapter class in initWidget(). Figure 4② uses one of the various construction method formats of the ArrayAdapter class,
public ArrayAdapter (Context context, 
                int resource, 
                T[] objects)
Among them, the parameter context indicates the context; the parameter resource specifies the layout file name of the Spinner appearance, android.R.layout.simple_spinner_item is a layout file defined by Android Studio, which specifies the appearance of the Spinner control; the parameter objects Represents the data in ArrayAdapter, and array_colors is the string array defined in Figure 4①.

2.2.2 Associating the SpinnerAdapter with the Spinner control

Next, in initWidget(), associate the SpinnerAdapter with the Spinner control, the code is shown in Figure 5.
 
 
Figure 5 SpinnerAdapter is associated with the Spinner control

Among them, sp1 is a variable associated with the Spinner control, and the setAdapter() method of the Spinner class is called through sp1, and the parameter of this method is a variable of the SpinnerAdapter type. Through the code shown in Figure 5, associate the SpinnerAdapter with the Spinner control. In this way, the subitem data to be displayed in the Spinner control is specified. Run the program, and the effect shown in Figure 1 can be displayed at this time.

Guess you like

Origin blog.csdn.net/hou09tian/article/details/127588031