When we go to a restaurant, the waiter will take the menu and show us what to order. Today I’m going to share a simple selection dialog that shows the user a list of choices.

The implementation idea is as follows:

  1. Since there is a selection list, the contents of that list must be stored somewhere

  2. After the user selects a certain item, the user is prompted. What was selected just now

This function is mainly used by AlertDialog, source code is as follows:

1. Main Activity (look at the detailed comments in the code)

import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.memorydemo.R; public class AlertDialogDemo extends Activity { @Override protected void onCreate(Bundle onSavedInstance) { super.onCreate(onSavedInstance); setContentView(R.layout.alert_dialog_demo); Button button = findViewById(R.id.buttonAlertDialog); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Alertdialog.builder (AlertDialogDemo.this).setTitle("Please choose") I'm going to pass an array type, Such as r.array or String[].setitems (r.array.items_alert_dialog, New DialogInterface. An OnClickListener () {/ / click any item on the list @ Override public void onClick (DialogInterface dialog, int which) { String[] items = getResources().getStringArray(R.array.items_alert_dialog); New AlertDialog.Builder(AlertdialogDemo. this) // Again pop-up box, remind user to select the content. SetMessage ("You chose: " + items[which]) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); }}) // The first dialog box has a cancel button. SetNegativeButton (Android.r.sing.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .show(); }}); }}Copy the code

2. Layout file alert_dialog_demo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:paddingTop="20dp" Android :layout_width="match_parent" Android :gravity="center" android:gravity="center" Android :layout_height="wrap_content" Android :id="@+id/textView7"/> <Button Android :text=" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonAlertDialog"/> </LinearLayout>Copy the code

3. The list contents are defined in res/values/strings.xml:

<resources> ... <array name="items_alert_dialog"> <item> <item> <item> <item> <item>Copy the code

4. The effect picture is as follows:

The purpose of sharing this extremely simple functionality is to lay a solid foundation for learning the intermediate and advanced uses of AlertDialog and realizing the need for complex selection capabilities.