移动应用开发技术-实验2.3

本文通过实例代码展示了如何在Android中使用AlertDialog创建不同类型的对话框,包括确认、列表、单选和多选对话框,并给出了自定义对话框的简单实现。同时,还演示了如何利用ProgressDialog显示圆形和水平进度条,提供用户友好的加载反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用 AlertDialog 实现如下程序界面和功能,如图 7 所示。

这个实验操作起来相对容易一些,书上有相同的例子,可以仿照书上看看自己编写一下。(图标图片自己找吧,我这就不说了)

<?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"
    android:gravity="center_horizontal">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name1"
        style="?android:attr/buttonBarButtonStyle"
        android:text="确认对话框" />
    <Button
        android:id="@+id/name2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="列表对话框" />
    <Button
        android:id="@+id/name3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="单选对话框" />
    <Button
        android:id="@+id/name4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="多选对话框" />
    <Button
        android:id="@+id/name5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="自定义对话框"/>
</LinearLayout>


package com.example.myapplication3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button Alert=null;
    private Button butAlert=null;
    private Button list=null;
    private Button single=null;
    private Button check=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.Alert=(Button) super.findViewById(R.id.name1);
        this.butAlert=(Button) super.findViewById(R.id.name5);
        this.list=(Button) super.findViewById(R.id.name2);
        this.single=(Button) super.findViewById(R.id.name3);
        this.check=(Button) super.findViewById(R.id.name4);
        this.Alert.setOnClickListener(new AlterImpl());
        this.butAlert.setOnClickListener(new butAlterImpl());
        this.list.setOnClickListener(new listImpl());
        this.single.setOnClickListener(new singleImpl());
        this.check.setOnClickListener(new editImpl());}



    private class AlterImpl implements View.OnClickListener {
        public void onClick(View v){
            Dialog dialog =new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.alert).setTitle("确定删除?").setMessage("您确定删除这个文件吗?").setPositiveButton("确定",new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,int whichButton){ }}).setNeutralButton("查看文件", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) { }}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) { }}).create();
            dialog.show(); }
    }

    private class listImpl implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            final CharSequence[] items={"苹果","芒果","草莓","香蕉"};
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("你喜欢吃哪种水果?");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(),"您的选择是;"+items[item],Toast.LENGTH_SHORT).show(); }});
            AlertDialog alert=builder.create();
            alert.show();}}

    private class singleImpl implements View.OnClickListener {
        public void onClick(View v){
            final CharSequence[]items ={"苹果","芒果","香蕉","草莓"};
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("你喜欢吃哪种水果?");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(),"您的选择是;"+items[item],Toast.LENGTH_SHORT).show(); }});
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { }
            });
            AlertDialog alert=builder.create();
            alert.show();}
    }

    private class editImpl implements View.OnClickListener {
        boolean[] flags=new boolean[]{false,false,false,false};
        public void onClick(View v){
            final CharSequence[]items ={"苹果","芒果","香蕉","草莓"};
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("你喜欢吃哪种水果?");
            boolean[] falgs=new boolean[]{false,false,false,false};;
            builder.setMultiChoiceItems(items, falgs,new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    flags[which]=isChecked;
                    String result="您的选择是:";
                    for(int i=0;i<flags.length;i++)
                    {
                        if(flags[i]){
                            result=result+items[i]+"、";
                        }
                    }
                 Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();}});
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            AlertDialog alert=builder.create();
            alert.show();}
                }


    private class butAlterImpl implements View.OnClickListener {
        public void onClick(View v){
            LayoutInflater flater=LayoutInflater.from(MainActivity.this);
            View dialogView=flater.inflate(R.layout.login,null);
            Dialog dialog= new AlertDialog.Builder(MainActivity.this).setTitle("用户登录界面").setView(dialogView).setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create();
            dialog.show();

        }
    }
}

自定义对话框中实现的是简易的登录页面,大家应该会写吧,不会写代码在下方
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <TextView
            android:text="用户名:"
            android:layout_marginLeft="18dip"
            android:textSize="10pt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <EditText
            android:width="65pt"
            android:layout_height="wrap_content"/>
    </TableRow>
        <TableRow>
            <TextView
                android:text="密 码:"
                android:layout_marginLeft="18dip"
                android:textSize="10pt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:password="true"
                android:width="65pt"
                android:layout_height="wrap_content"/>
        </TableRow>
</TableLayout>


使用 ProgressDialog 实现如下程序界面和功能,如图 13 所示。(图不截了,直接上代码)
<?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"
   android:gravity="center_horizontal">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/circleBut"
        android:text="圆形进度条"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lineBut"
        android:text="水平进度条"/>

</LinearLayout>
package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {

    private Button circleBut,lineBut;
    int count=0;
    ProgressDialog ProDialog;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        circleBut=(Button)this.findViewById(R.id.circleBut);
        lineBut=(Button)this.findViewById(R.id.lineBut);
        circleBut.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                ProDialog=new ProgressDialog(MainActivity2.this);
                ProDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                ProDialog.setTitle("提示");
                ProDialog.setMessage("这是一个圆形进度条对话框");
                ProDialog.setIndeterminate(false);
                ProDialog.setCancelable(true);
                ProDialog.setButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ProDialog.cancel(); }});
                        ProDialog.show();
            }
        });
        lineBut.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
            count=0;
            ProDialog=new ProgressDialog(MainActivity2.this);
            ProDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            ProDialog.setTitle("提示");
            ProDialog.setMessage("这是一个长条形进度条对话框");
            ProDialog.setIndeterminate(false);
            ProDialog.setCancelable(true);
            ProDialog.show();
            new Thread(){
                public void run(){
                    while(count<=100){
                        ProDialog.setProgress(count++);
                        try { Thread.sleep(100);}
                        catch(InterruptedException e){
                            e.printStackTrace(); } }
                    ProDialog.cancel(); }}.start(); }});
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值