Android期末复习4-4:使用对话框AlertDialog

题目

在该应用中,活动页面视图根节点为垂直的LinearLayout,在布局中依次有1个TextView,显示个人信息;1个TextView(id为tv_result),用于显示对话框选项结果;3个Button,分别显示“消息对话框”“列表对话框”和“单选对话框”。点击“消息对话框”按钮,弹出消息对话框,显示提示消息。点击“列表对话框”按钮,弹出列表对话框,显示“游泳”、“跑步”和“篮球”3个爱好选项,点击选项,文本框tv_result更新为所选内容,对话框消失,若是点击“退出”按钮,则对话框直接消失。点击“单选对话框”按钮,弹出单选对话框,选项为所列的3个爱好,点击选项,文本框tv_result没有立即更新,点击“确定”按钮后,文本框更新为所选内容,对话框消失,若点击“退出”按钮,则对话框直接退出,不改变tv_result的内容。

实现效果

步骤

1.新建项目,更改布局文件为my_main.xml

删除app/re/layout/activity_main.xml文件,新建自己的my_main.xml布局文件,其中Root Element设置为LinearLayout,改MainActivity.java中的setContentView(R.layout.my_main)

 

2.对my_main.xml操作 

 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="周冬玲21211870234" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="消息对话框" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表对话框" />

    <Button
        android:id="@+id/bt3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框" />
</LinearLayout>

3.MainActivity.java部分

package com.example.zdl_task4_4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //1.声明TextView,将其作为成员变量供多个方法调用
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);

        //2.在onCreate中找到TextView的id,设置列表框和单选框展示的内容,放到String数组里面作为参数传递
        tv=findViewById(R.id.tv_result);
        String[] hobbies=new String[]{"游泳","跑步","篮球"};

        //3.实现消息提示框的功能
        //(1)给button设置点击侦听器并调用showMessageDialog方法(手动创建)
        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {//找到button的id,为其设置点击侦听器setOnClickListener,传入一个匿名内部类("new "+enter键)
            @Override
            public void onClick(View v) {
                //调用展示消息提示框方法(自定义方法名),传入String类型参数,作为消息提示框的title
                showMessageDialog("这是一个消息提示框");//alt+enter键弹出提示,选中第一个Create method,按enter,再次弹出提示,选择第二个在MainActivity中创建方法,按enter
            }
        });

        //4.实现列表框的功能
        //(1)给button设置点击侦听器并调用showItemDialog方法(手动创建)
        findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {//找到button的id,为其设置点击侦听器setOnClickListener,传入一个匿名内部类("new "+enter键)
            @Override
            public void onClick(View v) {
                //调用展示列表框方法(自定义方法名),传入String类型数组,作为列表的选项列表
                showItemDialog(hobbies);//alt+enter键弹出提示,选中第一个Create method,按enter,再次弹出提示,选择第二个在MainActivity中创建方法,按enter
            }
        });

        //5.实现单选框的功能
        //(1)给button设置点击侦听器并调用showSingleChoiceDialog方法(手动创建)
        findViewById(R.id.bt3).setOnClickListener(new View.OnClickListener() {//找到button的id,为其设置点击侦听器setOnClickListener,传入一个匿名内部类("new "+enter键)
            @Override
            public void onClick(View v) {
                //调用展示单选对话框方法(自定义方法名),传入String类型数组,作为单选对话框的选项列表
                showSingleChoiceDialog(hobbies);//alt+enter键弹出提示,选中第一个Create method,按enter,再次弹出提示,选择第二个在MainActivity中创建方法,按enter
            }
        });
    }

    //3.实现消息提示框的功能
    //(2)创建showMessageDialog方法,设置消息提示框的内容
    private void showMessageDialog(String message) {
        //new一个AlertDialog.Builder对象
        AlertDialog.Builder bl = new AlertDialog.Builder(this);
        //setTitle设置对话框标题,setMessage设置对话框文本内容
        //setNegativeButton:设置取消按钮,第一个参数是按钮所显示的文本,第二个参数是对应的点击事件接口,若不需要处理点击事件,可传入null
        //show方法显示对话框,一定不要忘了!!!
        bl.setTitle("消息提示框").setMessage(message).setNegativeButton("退出",null).show();
    }

    //4.实现列表框的功能
    //(2)创建showItemDialog方法,设置列表框内容
    private void showItemDialog(String[] hobbies) {
        //new一个AlertDialog.Builder对象
        AlertDialog.Builder bl = new AlertDialog.Builder(this);
        bl.setTitle("列表框");//setTitle设置对话框标题
        bl.setItems(hobbies, new DialogInterface.OnClickListener() {//setItem()设置列表数据和点击侦听(括号中"new "+enter键)
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String hobby = hobbies[which];//找到被选中的列表项
                tv.setText(hobby);//显示在TextView上
            }
        }).setNegativeButton("退出",null).show();//setNegativeButton设置取消按钮,show()显示对话框,千万不要忘记show!!!
    }

    //5.实现单选框的功能
    //(2)创建showSingleChoiceDialog方法,设置单选框内容
    private void showSingleChoiceDialog(String[] hobbies) {
        AlertDialog.Builder bl = new AlertDialog.Builder(this);//new一个AlertDialog.Builder对象
        bl.setTitle("单选对话框");//setTitle设置单选对话框标题

        //setSingleChoiceItems设置单选数据、默认选中位置和点击侦听,分别对应它的三个参数,但是有一个问题
        // 选中某个数据的时候,onClick方法中会返回一个which,但是这个which在外部不能被访问,
        // 而我们在选择单选框选项的时候是在setPositiveButton方法中设置的,必须要把它传到setPositiveButton方法中去,所以想到设置一个MyInteger类,存放被选中的位置
        MyInteger myInteger=new MyInteger(0);//先去创建一个MyInteger类,才能new出MyInteger实例
        bl.setSingleChoiceItems(hobbies, myInteger.getPosition(), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myInteger.setPosition(which);//把被选中的选项位置通过setPosition方法,存到myInteger中
            }
            //setPositiveButton设置确定按钮,第一个参数是按钮所显示的文本,第二个参数是对应的点击事件接口,若不需要处理点击事件,可传入null
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {//选中后不会自动退出,需增加一个确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int position = myInteger.getPosition();//获取选中的选项位置
                tv.setText(hobbies[position]);//将文本显示到TextView中
            }
        }).setNegativeButton("退出",null).show();//设置取消按钮,别忘了show!!!
    }

    //创建MyInteger类
    class MyInteger{
        int position;//设置成员变量position存放被选中选项的位置

        public MyInteger(int position) {//智能生成构造方法(code->generate->Constructor)
            this.position = position;
        }
        //智能生成get和set方法(code->generate->Getter和Setter)
        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
}

重点总结

稍后再总结

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值