欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

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

来源: 开发者 投稿于  被查看 29385 次 评论:244

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效果

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

您可能感兴趣的文章:
  • Android Studio实现单选对话框
  • Android studio自定义对话框效果
  • Android中自定义对话框(Dialog)的实例代码
  • Android实现底部对话框BottomDialog弹出实例代码
  • Android自定义对话框Dialog的简单实现
  • 详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限
  • Android实现点击AlertDialog上按钮时不关闭对话框的方法
  • 实例详解Android自定义ProgressDialog进度条对话框的实现
  • Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
  • Android中AlertDialog各种对话框的用法实例详解

用户评论