Implementation of Android dialog (above) setting layout implementation

The code content of this article is to implement a custom dialog by setting the layout, and the implementation method of updating the Android dialog every day or the day after tomorrow (below) is realized by calling the interface method (this method is more efficient than the previous method At the same time, the difficulty is relatively large) The following is the code content:

Set two buttons in activity_main.xml, the name of the button is the method of implementation, the function of the first button is the implementation method of this content, and the implementation of the second button function will use another method, so The implementation method of defining settings with two dialog boxes in one program can accumulate more knowledge and understand the knowledge points of different Android codes!

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过设置布局实现自定义对话框"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="完全自定义对话框"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

In the establishment of the Hugo layout_dialog.xml layout file is placed the interface layout that clicks the first button to jump out

<?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="240dp"
    android:background="@color/colorGreen"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="***提示***"
        android:textSize="28sp"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:layout_marginTop="10dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimaryDark"/>
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="你确定要退出当前应用吗?"
        android:textSize="18sp"
        android:gravity="center"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimaryDark"/>
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <Button
           android:id="@+id/btn_cancle"
           android:layout_width="140dp"
           android:layout_height="wrap_content"
           android:text="取消"
           android:background="#aaa"
           android:layout_marginTop="10dp"
           android:layout_marginLeft="20dp"
           />
       <Button
           android:id="@+id/btn_ok"
           android:layout_width="140dp"
           android:layout_height="wrap_content"
           android:text="确定"
           android:background="@color/colorPrimary"
           android:layout_marginTop="10dp"
           android:layout_marginLeft="90dp"
           />
   </LinearLayout>
</LinearLayout>

 

In MainActivity, java places the settings of various properties of the dialog and calls to the layout_dialog.xml interface

package com.example.myapplication;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button btn = this.findViewById(R.id.btn);
        btn.setOnClickListener(this);

        Button btn2 = this.findViewById(R.id.btn1);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn:
                createDialogbyLayout();
                break;
            case R.id.btn1:
                createDialogbyCustomDialog();
                break;
        }

    }
    //该方法本次没有用到,每天会用到
    private void createDialogbyCustomDialog() {
            CustomDialog dialog =new CustomDialog(this);
            dialog.setOnClickBottomListener(new CustomDialog.OnClickBottomListener() {
                @Override
                public void onPositiveClick() {


                }

                @Override
                public void onNegativeClick() {

                }
            });
    }

    private void createDialogbyLayout() {
        //1.创建构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //2.获得布局
        View view_dialog = View.inflate(this,R.layout.layout_dialog,null);
        builder.setView(view_dialog);
        //3.创建对话框
        final AlertDialog dialog = builder.create();
        //4.设置自定义对话框的按钮事件
        Button  btnCancle = view_dialog.findViewById(R.id.btn_cancle);
        Button btnOK = view_dialog.findViewById(R.id.btn_ok);

        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.this.finish();
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

 

 

 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/123809278