对话框的点击事件

几种常用的对话框:普通对话框,列表对话框,单选对话框,多选对话框



xml布局:

<?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:background="#ffffff"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@drawable/re_corners"
        android:gravity="center"
        android:text="普通Dialog" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@drawable/re_corners"
        android:gravity="center"
        android:text="列表Dialog" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@drawable/re_corners"
        android:text="单选Dialog" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:background="@drawable/re_corners"
        android:gravity="center"
        android:text="多选Dialog" />
</LinearLayout>
activity的实现:
public class SixActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv1,tv2,tv3,tv4;
    private boolean[] checkItems;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_six);
        initView();

    }

    private void initView() {
        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);
        tv3=findViewById(R.id.tv3);
        tv4=findViewById(R.id.tv4);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
        //普通对话框
            case R.id.tv1:
                new android.app.AlertDialog.Builder(SixActivity.this)
                        .setTitle("系统提示")
                        .setMessage("这是一个普通的对话框")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override//
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(SixActivity.this, "确定", Toast.LENGTH_SHORT).show();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Toast.makeText(SixActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            //普通列表对话框
            case R.id.tv2:
                final String[] book = new String[]{"篮球", "排球", "羽毛球", "足球"};
                new AlertDialog.Builder(SixActivity.this)
                        .setTitle("选择你喜欢的球类")
                        .setItems(book, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int which) {
                                Toast.makeText(getApplicationContext(), "你选择了" + book[which], Toast.LENGTH_SHORT).show();
                            }
                        }).show();
                   break;
            //单选列表对话框
            case R.id.tv3:
                final String[] color = new String[]{"白色", "红色", "蓝色", "绿色", "粉色"};
                new AlertDialog.Builder(SixActivity.this)
                        .setTitle("选择你喜欢的颜色,只能选一种哦")
                         .setSingleChoiceItems(color, 0,new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int which) {
                    Toast.makeText(getApplicationContext(), "你选择了" + color[which], Toast.LENGTH_SHORT).show();
                }
            }).show();
                break;
            //多选列表对话框
            case R.id.tv4:
                final String[] smoke = new String[]{"中华", "芙蓉王", "金典100", "迎春", "利群"};
                //定义一个用来记录个列表项状态的boolean数组
                checkItems = new boolean[]{false, false, false,false,false};
                new AlertDialog.Builder(SixActivity.this)
                        .setMultiChoiceItems(smoke, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {
                                checkItems[which] = isChecked;
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        String result = "";
                        for (int i = 0; i < checkItems.length; i++) {
                            if (checkItems[i])
                                result += smoke[i] + " ";
                        }
                        Toast.makeText(getApplicationContext(), "你喜欢:" + result, Toast.LENGTH_SHORT).show();
                    }
                }).show();
                    break;
        }
    }
}
一个drawable文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置透明背景色 -->
    <solid android:color="#d8ecf5" />

    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="50px"
        android:bottomRightRadius="50px"
        android:topLeftRadius="50px"
        android:topRightRadius="50px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

</shape>

就这样了,是不是很简单,。

源码下载地址:https://download.csdn.net/download/weixin_42267745/10443535




猜你喜欢

转载自blog.csdn.net/weixin_42267745/article/details/80488782
今日推荐