【Android初级】如何实现一个具有选择功能的对话框效果

454 阅读1分钟

我们去餐厅吃饭时,服务员都会拿菜单给我们选择点什么菜。今天就分享一个具有选择功能的简易对话框,给用户展示一个选择列表。

实现思路如下:

  1. 既然有选择列表,那么这个列表的内容肯定保存在某个地方

  2. 用户选择某一项后,给用户做出提示,刚才选择的是什么

该功能主要用的是 AlertDialog,源码如下:

1、主Activity(细看代码中详细的注释)

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")

                        // 设置对话框的显示内容,是个内容列表,注意:这里需要传数组类型,比如 R.array 或者 String[]
                        .setItems(R.array.items_alert_dialog, new DialogInterface.OnClickListener() {

                            // 点击列表上的任一项
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String[] items = getResources().getStringArray(R.array.items_alert_dialog);
                                new AlertDialog.Builder(AlertDialogDemo.this)

                                        // 再次弹框,向用户提示 用户刚才选择的内容
                                        .setMessage("You chose: " + items[which])
                                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                            }
                                        }).show();
                            }
                        })

                        // 第一个对话框有个 取消 按钮
                        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
    }

}

2、布局文件 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:text="这是一个AlertDialog样例"
            android:layout_width="match_parent"
            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>

3、列表内容定义在 res/values/strings.xml 中:

<resources>
    
  ...
  
    <array name="items_alert_dialog">
        <item>成都串串</item>
        <item>重庆火锅</item>
        <item>港式餐厅</item>
    </array>

</resources>

4、效果图如下:

分享这个极为简单的功能,主要是为后面学习AlertDialog的中高级用法以及实现具备复杂选择功能的需求打下坚实的基础。