Android Studio使用自定义对话框效果

 更新时间:2022年05月17日 17:20:19   作者:徐为波  
这篇文章主要为大家详细介绍了Android Studio使用自定义对话框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Android Studio基础使用自定义对话框,供大家参考,具体内容如下

兼容低版本的APP运行

第一步:新建新的空白activity,布局XML设置如下

该APP的启动界面activity_main.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=".MainActivity">
 
    <Button
        android:id="@+id/btn_single"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示单选对话框"/>
    <Button
        android:id="@+id/btn_alert_customer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义对话框"/>
 
</LinearLayout>

自定义的对话框item_alert_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=".MainActivity">
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义Dialog界面!"
        />
    <TextView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="@mipmap/ic_launcher"/>
 
</LinearLayout>

第二步:在MainActivity.java中绑定布局xml

第三步:对话框实现抽象方式

package com.xwb.zdydhk;

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.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_single = findViewById(R.id.btn_single);
        btn_single.setOnClickListener(this);
        Button btn_alert_customer = findViewById(R.id.btn_alert_customer);
        btn_alert_customer.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        switch (v.getId()){
            case R.id.btn_single:
                builder.setTitle("单选对话框").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(new String[]{"中国", "德国", "日本"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"选中的"+which,Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_alert_customer:
                //setView(R.layout.item_alert_dialog)为自定义的对话框,或图片等等
                builder.setTitle("自定义对话框").setIcon(R.mipmap.ic_launcher).setView(R.layout.item_alert_dialog);
                break;
            }
        AlertDialog ad = builder.create();
        ad.show();
    }
}

第四步:运行APP效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android 广播监听网络状态详解及实例代码

    Android 广播监听网络状态详解及实例代码

    这篇文章主要介绍了Android 广播监听网络状态详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Kotlin类与属性及构造函数的使用详解

    Kotlin类与属性及构造函数的使用详解

    这篇文章主要介绍了Kotlin语言中类与属性及构造函数的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Android Studio实现补间动画

    Android Studio实现补间动画

    这篇文章主要为大家详细介绍了Android Studio实现补间动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Android实现背景图滑动变大松开回弹效果

    Android实现背景图滑动变大松开回弹效果

    这篇文章主要为大家详细介绍了Android实现背景图滑动变大松开回弹效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android Fragment的生命周期详解

    Android Fragment的生命周期详解

    本文主要讲解Android Fragments的生命周期,这里整理了相关资料及对Fragment 的创建,有需要的小伙伴看下
    2016-09-09
  • android通过led实现手电筒功能

    android通过led实现手电筒功能

    这篇文章主要为大家详细介绍了android通过led实现手电筒功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android 实现截屏功能的实例

    Android 实现截屏功能的实例

    这篇文章主要介绍了Android 实现截屏功能的实例的相关资料,这里实现截屏的实例在代码中注释非常清楚,希望能帮助到大家,需要的朋友可以参考下
    2017-08-08
  • Android 调用百度地图API示例

    Android 调用百度地图API示例

    在Android开发中有一个非常重要的应用就是实时定位,通过手机在手机地图上进行实时定位,定位当前手机的位置,这篇文章主要介绍了Android 调用百度地图API示例,有兴趣的可以了解一下。
    2017-01-01
  • Android 中 Fragment的使用大全

    Android 中 Fragment的使用大全

    通常地 fragment做为宿主activity UI的一部分, 被作为activity整个view hierarchy的一部分被嵌入。本篇文章给大家介绍android fragment使用,需要的朋友一起看看吧
    2015-10-10
  • 详解Android观察者模式的使用与优劣

    详解Android观察者模式的使用与优劣

    这篇文章主要介绍了Android观察者模式的相关资料,帮助大家更好的理解和学习Android的设计模式,感兴趣的朋友可以了解下
    2020-09-09

最新评论