zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(弹出类): AlertDialog 自定义

    示例如下:

    /view/flyout/AlertDialogDemo2.java

    /**
     * AlertDialog 自定义,即自定义 AlertDialog 显示的 view
     *
     * 通过 AlertDialog.Builder 的 setView() 来指定 AlertDialog 需要显示的 view
     *
     * 本例的自定义 view 的布局文件请参见:res/layout/alertdialog_view_flyout_alertdialogdemo2.xml
     */
    
    package com.webabcd.androiddemo.view.flyout;
    
    import android.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.webabcd.androiddemo.R;
    
    public class AlertDialogDemo2 extends AppCompatActivity {
    
        private Button mButton1;
    
        private AlertDialog mAlert;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_flyout_alertdialogdemo2);
    
            mButton1 = findViewById(R.id.button1);
    
            sample();
        }
    
        private void sample() {
            // 从布局文件中加载 AlertDialog 需要显示的 view
            LayoutInflater inflater = this.getLayoutInflater();
            View customView = inflater.inflate(R.layout.alertdialog_view_flyout_alertdialogdemo2, null,false);
    
            AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo2.this);
            // 指定 AlertDialog 需要显示的 view
            builder.setView(customView);
            // 点击空白处是否自动隐藏对话框(默认值为 true)
            builder.setCancelable(false);
            // 创建 AlertDialog 对象
            mAlert = builder.create();
    
            // 弹出自定义对话框
            mButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlert.show();
                }
            });
    
    
    
            // 设置自定义 view 中的显示内容
            ((TextView) customView.findViewById(R.id.textViewTitle)).setText("标题");
            ((TextView) customView.findViewById(R.id.textViewContent)).setText("内容");
            ((Button) customView.findViewById(R.id.buttonClose)).setText("X");
            ((Button) customView.findViewById(R.id.buttonConfirm)).setText("确认");
            ((Button) customView.findViewById(R.id.buttonCancel)).setText("取消");
    
            // 自定义 view 中的关闭按钮的点击事件
            customView.findViewById(R.id.buttonClose).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(AlertDialogDemo2.this, "点击了关闭按钮", Toast.LENGTH_SHORT).show();
                    mAlert.dismiss();
                }
            });
    
            // 自定义 view 中的确认按钮的点击事件
            customView.findViewById(R.id.buttonConfirm).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(AlertDialogDemo2.this, "点击了确认按钮", Toast.LENGTH_SHORT).show();
                    mAlert.dismiss();
                }
            });
    
            // 自定义 view 中的取消按钮的点击事件
            customView.findViewById(R.id.buttonCancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(AlertDialogDemo2.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                    mAlert.dismiss();
                }
            });
        }
    }
    
    

    /layout/activity_view_flyout_alertdialogdemo2.xml

    <?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">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="弹出一个自定义 view 的 AlertDialog" />
    
    </LinearLayout>
    
    

    /layout/alertdialog_view_flyout_alertdialogdemo2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--
        自定义 AlertDialog 显示的 view(参见:/view/flyout/AlertDialogDemo2.java)
    -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <RelativeLayout
            android:id="@+id/layoutTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@color/red"
            android:padding="5dp">
    
            <TextView
                android:id="@+id/textViewTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="title"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <Button
                android:id="@+id/buttonClose"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:padding="0dp"
                android:text="close"
                android:textSize="14sp" />
    
        </RelativeLayout>
    
        <LinearLayout
            android:id="@+id/layoutDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layoutTitle"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/textViewContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:text="content"
                android:textColor="@color/green"
                android:textSize="18sp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layoutDetail"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/buttonConfirm"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:text="confirm"
                android:textColor="@color/blue"
                android:textSize="16sp" />
    
            <Button
                android:id="@+id/buttonCancel"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:text="cancel"
                android:textColor="@color/blue"
                android:textSize="16sp" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    手把手实战:eclipse 搭建 SpringMvc 框架环境
    解决eclipse中Tomcat服务器的server location选项不能修改的问题
    如何解决JSP页面顶端报错 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    第二课 --- git的(管理修改和撤销修改、删除文件)
    第二课 ---git时光穿梭(版本回退)
    第一课——git的简介和基本使用
    001——使用composer安装ThinkPHP5
    微信小程序中对于变量的定义
    微信小程序onLaunch修改globalData的值
    7——ThinkPhp中的响应和重定向:
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_flyout_AlertDialogDemo2.html
Copyright © 2011-2022 走看看