android学习记录(四)

本文详细介绍了Android中AlertDialog的使用,包括普通对话框、带列表的对话框、自定义对话框、带进度条的对话框以及带日期选择的对话框的实现方法,展示了如何创建并设置各种类型的对话框,以及在不同场景下的应用。
摘要由CSDN通过智能技术生成

对话框AlertDialog的使用

普通对话框
activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DialogActivity"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start_Normal_AlertDialog"
        android:id="@+id/start_Normal_AlertDialog"
        android:onClick="startNormalDialog1"/>

</LinearLayout>
DialogActivity
package com.example.uicomponenttest;

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

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }

    public void startNormalDialog1(View view){
        new AlertDialog.Builder(this).setTitle("删除记录确定").setMessage("是否删除该条记录").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"删除记录成功!!!",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"取消删除记录!!!",Toast.LENGTH_SHORT).show();
            }
        }).show();//setNegativeButton中 第一参数有两种情况(单引号表示textid,双引号表示text)
    }
}
带列表对话框
activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DialogActivity"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动常规对话框"
        android:id="@+id/start_Normal_AlertDialog"
        android:onClick="startNormalDialog1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_List_AlertDialog"
        android:onClick="startListDialog"
        android:text="启动带有列表的对话框"/>


</LinearLayout>
DialogActivity
package com.example.uicomponenttest;

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

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
    public void startNormalDialog1(View view){
        new AlertDialog.Builder(this).setTitle("删除记录确定").setMessage("是否删除该条记录").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"删除记录成功!!!",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"取消删除记录!!!",Toast.LENGTH_SHORT).show();
            }
        }).show();//setNegativeButton中 第一参数有两种情况(单引号表示textid,双引号表示text)
    }
    public void startListDialog(View view){
        String[] colors={"Green","blue","red","yellow","black"};
        new AlertDialog.Builder(this).setTitle("请选择一种颜色").setSingleChoiceItems(colors, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,colors[which],Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).setPositiveButton("ok",null).show();
    }
}
自定义对话框
dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/name"
       android:hint="用户名"
       />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="numberPassword"/>

</LinearLayout>
DialogActivity
package com.example.uicomponenttest;

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

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
    public void startNormalDialog1(View view){
        new AlertDialog.Builder(this).setTitle("删除记录确定").setMessage("是否删除该条记录").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"删除记录成功!!!",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"取消删除记录!!!",Toast.LENGTH_SHORT).show();
            }
        }).show();//setNegativeButton中 第一参数有两种情况(单引号表示textid,双引号表示text)
    }
    public void startListDialog(View view){
        String[] colors={"Green","blue","red","yellow","black"};
        new AlertDialog.Builder(this).setTitle("请选择一种颜色").setSingleChoiceItems(colors, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,colors[which],Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).setPositiveButton("ok",null).show();
    }
    public void startCustomDialog(View view){
        View v=View.inflate(this,R.layout.dialog_view,null);
         EditText name=v.findViewById(R.id.name);
         EditText psw=v.findViewById(R.id.password);
        new AlertDialog.Builder(this).setTitle("登录").setView(v).setPositiveButton("登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"用户名"+name.getText().toString().trim()+"密码"+psw.getText().toString().trim(),Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).show();
    }
}
带进度条的对话框
dialogActivity
package com.example.uicomponenttest;

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

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

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
    public void startNormalDialog1(View view){
        new AlertDialog.Builder(this).setTitle("删除记录确定").setMessage("是否删除该条记录").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"删除记录成功!!!",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"取消删除记录!!!",Toast.LENGTH_SHORT).show();
            }
        }).show();//setNegativeButton中 第一参数有两种情况(单引号表示textid,双引号表示text)
    }
    public void startListDialog(View view){
        String[] colors={"Green","blue","red","yellow","black"};
        new AlertDialog.Builder(this).setTitle("请选择一种颜色").setSingleChoiceItems(colors, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,colors[which],Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).setPositiveButton("ok",null).show();
    }
    public void startCustomDialog(View view){
        View v=View.inflate(this,R.layout.dialog_view,null);
         EditText name=v.findViewById(R.id.name);
         EditText psw=v.findViewById(R.id.password);
        new AlertDialog.Builder(this).setTitle("登录").setView(v).setPositiveButton("登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"用户名"+name.getText().toString().trim()+"密码"+psw.getText().toString().trim(),Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).show();
    }
    public void startProgressDialog(View view){
        final  ProgressDialog progressDialog=new ProgressDialog(this);
        progressDialog.setTitle("带进度条的对话框");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1; i<=20; i++){
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress()+5);
                }
                progressDialog.dismiss();
            }
        }).start();

    }
}
activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DialogActivity"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动常规对话框"
        android:id="@+id/start_Normal_AlertDialog"
        android:onClick="startNormalDialog1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_List_AlertDialog"
        android:onClick="startListDialog"
        android:text="启动带有列表的对话框"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_custom_AlertDialog"
        android:onClick="startCustomDialog"
        android:text="启动自定义对话框"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_ProgressDialog"
        android:text="启动带进度条的对话框"
        android:onClick="startProgressDialog"/>



</LinearLayout>
带日期的对话框
DialogActivity
package com.example.uicomponenttest;

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

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
    public void startNormalDialog1(View view){
        new AlertDialog.Builder(this).setTitle("删除记录确定").setMessage("是否删除该条记录").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"删除记录成功!!!",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"取消删除记录!!!",Toast.LENGTH_SHORT).show();
            }
        }).show();//setNegativeButton中 第一参数有两种情况(单引号表示textid,双引号表示text)
    }
    public void startListDialog(View view){
        String[] colors={"Green","blue","red","yellow","black"};
        new AlertDialog.Builder(this).setTitle("请选择一种颜色").setSingleChoiceItems(colors, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,colors[which],Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).setPositiveButton("ok",null).show();
    }
    public void startCustomDialog(View view){
        View v=View.inflate(this,R.layout.dialog_view,null);
         EditText name=v.findViewById(R.id.name);
         EditText psw=v.findViewById(R.id.password);
        new AlertDialog.Builder(this).setTitle("登录").setView(v).setPositiveButton("登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this,"用户名"+name.getText().toString().trim()+"密码"+psw.getText().toString().trim(),Toast.LENGTH_SHORT).show();
            }
        }).setCancelable(false).show();
    }
    public void startProgressDialog(View view){
        final  ProgressDialog progressDialog=new ProgressDialog(this);
        progressDialog.setTitle("带进度条的对话框");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1; i<=20; i++){
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress()+5);
                }
                progressDialog.dismiss();
            }
        }).start();

    }
    public void startDailyDialog(View view){
        Calendar calendar =Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH);
        int day=calendar.get(Calendar.DAY_OF_MONTH);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(DialogActivity.this,"选中了: "+year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
            }
        },year,month,day).show();


    }
}
activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DialogActivity"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动常规对话框"
        android:id="@+id/start_Normal_AlertDialog"
        android:onClick="startNormalDialog1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_List_AlertDialog"
        android:onClick="startListDialog"
        android:text="启动带有列表的对话框"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_custom_AlertDialog"
        android:onClick="startCustomDialog"
        android:text="启动自定义对话框"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_ProgressDialog"
        android:text="启动带进度条的对话框"
        android:onClick="startProgressDialog"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/start_DailyDialog"
        android:text="启动带日期的对话框"
        android:onClick="startDailyDialog"/>


</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值