zoukankan      html  css  js  c++  java
  • Android基础-自定义对话框

    1. 设计自定义对话框样式-->dialog_layout.xml 

    使用的是线性布局:

       第一步: 设置文本TextView 

       第二步: 设置线性布局 使用图片做为点击事件 

    <?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"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:background="@mipmap/dialog_bg">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="真的要退出吗?"
            android:textSize="34sp"
            android:textColor="#e61414"
            android:textStyle="bold"
            android:layout_marginTop="265dp"/>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="25dp">
    
            <ImageView
                android:id="@+id/yes_btn"
                android:layout_width="120dp"
                android:layout_height="50dp"
                android:src="@mipmap/yes_btn"
                />
    
            <ImageView
                android:id="@+id/no_btn"
                android:layout_width="120dp"
                android:layout_height="50dp"
                android:src="@mipmap/no_btn"
             />
    
        </LinearLayout>
    </LinearLayout>

    2.设置对话框的背景style

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="mydialog" parent="android:style/Theme.Dialog">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
        </style>
    </resources>

    3.设置activity的Mydialog.java 

    package com.example.dialog;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.view.View;
    
    import androidx.annotation.NonNull;
    
    //1.设计自定义对话框样式-->dialog_layout.xml
    //2.设计style(去标题栏,去背景)
    //3.将第一步的布局应用到当前自定义对话框
    //4.实例化对话框(参数1:环境上下文 参数2:第二步创建的style R.style.mydialog),并展示show 
    public class Mydialog extends Dialog {
    
        public Mydialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
            setContentView(R.layout.dialog_layout);
    
            findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.exit(0);
                }
            });
    
            findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss(); //对话框消失
                }
            });
        }
    
    }

    4.实例化对话框并使用show进行展示 

    package com.example.dialog;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void showNormalDialog(){
            AlertDialog dialog = new AlertDialog.Builder(this).create();
            dialog.setTitle("提示");
            dialog.setMessage("您确定要退出程序吗?");
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            });
            dialog.show();
        }
    
    
        public void myClick(View view) {
            switch (view.getId()) {
                case R.id.normal_dialog_btn:
                    //1.实例化一个Buidler
                    //AlertDialog的构造方法时protected
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    //2.设置对话框样式
                    builder.setTitle("提示");
                    builder.setMessage("您确定退出程序吗?");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
                    //展示 
                    builder.setNegativeButton("取消", null);
                    builder.show();
    //                AlertDialog dialog = builder.create();
    //                dialog.show();
                    break;
    
                case R.id.diy_dialog_btn:
                    Mydialog md = new Mydialog(this, R.style.mydialog);
                    md.show();
                    break;
            }
        }
    }

       

  • 相关阅读:
    [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
    [Redux] Reducer Composition with Arrays
    [Redux] Writing a Todo List Reducer (Toggling a Todo)
    [Redux] Writing a Todo List Reducer (Adding a Todo)
    [Javascript] Advanced Reduce: Common Mistakes
    [Javascript] Advanced Reduce: Additional Reducer Arguments
    [Redux] Avoiding Object Mutations with Object.assign() and ...spread
    [Javascript] Introducing Reduce: Common Patterns
    AI-Info-Micron-Insight:工业 5.0,伟大的思想将殊途同归
    AI-Info-Micron-Insight:案例分析:美光使用数据和人工智能来发现、倾听和感觉
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/14584264.html
Copyright © 2011-2022 走看看