UI常用组件(三)——Android对话框处理

一、提示对话框

消息提示机制,常用于向用户传递信息、提示或警告用户的行为。通用的方法有

1、AlertDialog

  • setTitle
  • setMessage
  • create
  • show
<?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="wrap_content"
    tools:context=".MainActivity2"
    android:orientation="vertical">

    <Button
        android:id="@+id/normal_dialog_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示一个普通对话框"
        android:onClick="myClick"/>

    <Button
        android:id="@+id/diy_dialog_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="显示一个自定义对话框"
        android:onClick="myClick"/>

</LinearLayout>

package com.example.testapplication;

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

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

public class MainActivity2 extends AppCompatActivity {

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

    public void myClick(View view){
        switch (view.getId()){
            case R.id.normal_dialog_btn:
                //AlertDialog的构造方法时protected
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage("您确定退出程序吗? ");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });

                builder.setNegativeButton("取消", null);

                builder.show();
                break;
            case R.id.diy_dialog_btn:
                break;
        }
    }
}

UI常用组件(三)——Android对话框处理

二、自定义对话框

  • 设置布局
  • 设置Style
  • 自定义Dialog
  • 显示
<?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"
    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">
        <Button
            android:id="@+id/yes_btn"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:background="@mipmap/yes_btn"/>
        <Button
            android:id="@+id/no_btn"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:background="@mipmap/no_btn"
            android:layout_marginLeft="20dp"/>
    </LinearLayout>
</LinearLayout>
package com.example.testapplication;

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

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

public class MainActivity2 extends AppCompatActivity {

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

    public void myClick(View view){
        switch (view.getId()){
            case R.id.normal_dialog_btn:
                //AlertDialog的构造方法时protected
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage("您确定退出程序吗? ");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });

                builder.setNegativeButton("取消", null);

                builder.show();
                break;
            case R.id.diy_dialog_btn:
                MyDialog md = new MyDialog(this, R.style.mydialog);
                md.show();
                break;
        }
    }
}
package com.example.testapplication;

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 view) {
                System.exit(0);
            }
        });

        findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }
}

三、PopupWindow

  • 1、基础配置
  • 2、设置样式
  • 3、事件响应
  • 4、设置动画
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#00ffff"
    android:padding="2dp"
    >

    <TextView
        android:id="@+id/choose"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:text="选择"
        android:textColor="#ffffff"
        android:gravity="center"
        android:background="#000000"/>

    <View
        android:layout_width="2dp"
        android:layout_height="30dp"
        android:background="#00ffff" />

    <TextView
        android:id="@+id/choose_all"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:text="全选"
        android:textColor="#ffffff"
        android:gravity="center"
        android:background="#000000"/>

    <View
        android:layout_width="2dp"
        android:layout_height="30dp"
        android:background="#00ffff" />

    <TextView
        android:id="@+id/copy"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:text="复制"
        android:textColor="#ffffff"
        android:gravity="center"
        android:background="#000000"/>


</LinearLayout>
public void myClick(View view){
    switch (view.getId()){
        case R.id.normal_dialog_btn:
            //AlertDialog的构造方法时protected
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("您确定退出程序吗? ");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });

            builder.setNegativeButton("取消", null);

            builder.show();
            break;
        case R.id.diy_dialog_btn:
            MyDialog md = new MyDialog(this, R.style.mydialog);
            md.show();
            break;

        case R.id.popuo_window:
            showPopupWindow(view);
            break;

    }
}

// 设置popupWindow
public void showPopupWindow(View view) {
    // 准备弹窗所需要的视图对象
    View v = LayoutInflater.from(this).inflate(R.layout.popup_laylout, null);
    //1、实例化对象
    //参数1:用在弹窗中的View
    //参数2、3:弹窗的宽高
    //参数4(focusable):能否获取焦点
    final PopupWindow window = new PopupWindow(v, 600, 100, true);
    //2、设置(背景、动画)
    //设置背景 透明色
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    //设置能响应外部的点击事件
    window.setOutsideTouchable(true);
    //设置弹窗能显示点击事件
    window.setTouchable(true);

    //3、显示
    //参数1(anchor): 锚
    //参数2、3:相对与锚在x、y方向上的偏移量
    window.showAsDropDown(view);

    //为弹窗中的文本添加点击事件
    v.findViewById(R.id.choose).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity2.this,"选择",Toast.LENGTH_SHORT).show();
            window.dismiss();
        }
    });

    v.findViewById(R.id.choose_all).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity2.this,"点击全部",Toast.LENGTH_SHORT).show();
            window.dismiss();
        }
    });

    v.findViewById(R.id.copy).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity2.this,"复制",Toast.LENGTH_SHORT).show();
            window.dismiss();
        }
    });
}

UI常用组件(三)——Android对话框处理

 

四、ArrayAdapter

1、数据适配器对话框

数据适配器,只能用开显示单一的文本。

构造方法

ArrayAdapter(Context context, int resource, int textviewId, List<T> objects)

2、使用

array_item_layout.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="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/star"/>

    <TextView
        android:id="@+id/item_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试"
        android:layout_marginLeft="15dp"/>
</LinearLayout>
private void showArrayDialog() {
        final String[] items = {"Java","Mysql","Android","HTML","C","JavaScript"};
//        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items);
        ArrayAdapter adapter = new ArrayAdapter(this,R.layout.array_item_layout,R.id.item_txt,items);
        AlertDialog.Builder builer = new AlertDialog.Builder(this)
                .setTitle("编程")
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,items[i],Toast.LENGTH_SHORT).show();
                        dialogInterface.dismiss();
                    }
                });
        builer.show();
    }


  坐得住板凳,耐得住寂寞,守得住初心!

上一篇:2021-11-01


下一篇:Java-Builder模式