Learning "Android Studio Development Practice" (6) - drop-down box
background
Continue to learn how to use Android Studio here, write a drop-down list selection tool 1 , and learn the use of the drop-down box Spinner, the array adapter ArrayAdapter, and the simple adapter SimpleAdapter. Now I want to design a drop-down list selection tool, including 3 drop-down boxes, the content is the names of the six planets of water, gold, earth, fire, wood, and soil. The first one is displayed in a drop-down manner, and the second one is displayed in a dialog box using an array adapter ArrayAdapter List, the third simple adapter SimpleAdapter to display the list in the form of a dialog box, each list item includes a picture and name of a planet.
The use of the drop-down box Spinner
A spinner is a drop-down box used to select an item from a string of lists, similar to a combination of radio buttons1 . The spinnerMode attribute can be set in the layout file layout/activity_main.xml
to control how the drop-down list is displayed. dropdown means to display the list in the form of a drop-down box, and dialog means to display the list in the form of a dialog box. The layout code can be written like this
<Spinner
android:id="@+id/sp_stars"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:entries="@array/stars" />
The entries attribute is used to specify the content in the list in advance. When using it, you need to add the array representing the name of the asteroid to the resource file in values/stars.xml
advance
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="stars">
<item></item>
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
</string-array>
</resources>
The use of array adapter ArrayAdapter
You can use the Spinner.setAdapter() method in the code file to add an adapter for the drop-down box Spinner, and the adapter can be ArrayAdapter or SimpleAdapter 1 . When creating an ArrayAdapter object, you need to specify its layout (not the overall layout of the drop-down box list, but the layout of a single list item). At this time, you can create a new layout file layout/activity_list.xml. There is only one TextView in this file, as follows shown
<?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="80dp"
android:gravity="center"
android:textSize="18sp"
android:textColor="#006400">
</TextView>
The use of simple adapter SimpleAdapter
ArrayAdapter can only display text, while SimpleAdapter can display text and pictures at the same time. First create a new layout file layout/activity_simple_list.xml
and add the following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="18sp"
android:textColor="#bdb76b" />
</LinearLayout>
This means that each item in the dropdown list is a linear layout consisting of 1 image view and 1 text view.
Writing layout files
A total of 3 layout files are required:
layout/activity_main.xml
, place three drop-down boxes in sequence in the vertical direction
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/sp_stars_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:spinnerMode="dropdown"
android:orientation="vertical"
android:entries="@array/stars" />
<Spinner
android:id="@+id/sp_stars_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:spinnerMode="dialog" />
<Spinner
android:id="@+id/sp_stars_3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:spinnerMode="dialog" />
<TextView
android:id="@+id/tv_bbs"
android:layout_width="match_parent"
android:layout_height="400dp"
android:text="" />
</LinearLayout>
layout/activity_list.xml
, array adapter list item layout setting, code see abovelayout/activity_simple_list.xml
, simple adapter list item layout setting, code see above
Writing code files
The pictures of the 6 planets need to be copied to the src/main/res/drawable folder of the current project under the AndroidStudioProjects directory in advance.
package com.example.spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.*;
import android.os.Bundle;
import java.util.*;
import android.view.View;
import android.view.View.*;
import android.text.method.ScrollingMovementMethod;
public class MainActivity extends AppCompatActivity {
private TextView tv_bbs;
private Spinner sp_stars_1, sp_stars_2, sp_stars_3;
private String[] stars = {
"", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn"};
private int[] iconArray = {
android.R.color.transparent, R.drawable.mercury, R.drawable.venus, R.drawable.earth, R.drawable.mars, R.drawable.jupiter, R.drawable.saturn};
private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 在下拉框中选择之后,在文本框中显示。
tv_bbs = (TextView) findViewById(R.id.tv_bbs);
tv_bbs.setMovementMethod(new ScrollingMovementMethod());
OnLongClickListener clickL = new ClickLAction();
tv_bbs.setOnLongClickListener(clickL);
// 第一个下拉框
sp_stars_1 = (Spinner) findViewById(R.id.sp_stars_1);
sp_stars_1.setOnItemSelectedListener(new ClickIAction());
sp_stars_1.setSelection(0);
// 第二个下拉框
sp_stars_2 = (Spinner) findViewById(R.id.sp_stars_2);
ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.activity_list, stars);
sp_stars_2.setPrompt("Please select a star:");
sp_stars_2.setAdapter(starAdapter);
sp_stars_2.setSelection(0);
sp_stars_2.setOnItemSelectedListener(new ClickIAction());
// 第三个下拉框
sp_stars_3 = (Spinner) findViewById(R.id.sp_stars_3);
for(int i=0; i < iconArray.length; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("icon", iconArray[i]);
item.put("name", stars[i]);
list.add(item);
}
SimpleAdapter starSimpleAdapter = new SimpleAdapter(this, list, R.layout.activity_simple_list, new String[] {
"icon", "name"}, new int[] {
R.id.iv_icon, R.id.tv_name});
sp_stars_3.setPrompt("Please select a star:");
sp_stars_3.setAdapter(starSimpleAdapter);
sp_stars_3.setSelection(0);
sp_stars_3.setOnItemSelectedListener(new ClickIAction());
}
private class ClickIAction implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if ( arg2 != 0) {
String s = tv_bbs.getText() + "\nYou choose: " + stars[arg2];
tv_bbs.setText(s);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
private class ClickLAction implements OnLongClickListener {
@Override
public boolean onLongClick(View v) {
if (v.getId() == R.id.tv_bbs) {
tv_bbs.setText("");
}
return true;
}
}
}
operation result
Generate the apk file according to the method 2 explored before , and then transfer it to the mobile phone to run. The results are as follows:
Ouyang Shen. Android Studio Development Practice. Tsinghua University Press. 2017. ↩︎ ↩︎ ↩︎
Learning "Android Studio Development Practice" (1) - Hello World_Xiatangren's Blog-CSDN Blog_android studio learning program development↩︎