文件结构 这里写图片描述

运行效果 这里写图片描述

主要代码 MainActivity

package cn.edu.sicnu.dialogdemo;

import android.app.DialogFragment; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements MyInterface {

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


public void showdialog1(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("dialog 1");
    builder.setMessage("please choose yes or no?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "yes",Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show();
        }
    });

    builder.show();
}

public void showdialog2(View view){
    MyDialog myDialog = new MyDialog();
    myDialog.show(getFragmentManager(),"dialog2");

}

@Override
public void buttonYesClicked() {
    Toast.makeText(MainActivity.this, "I am acticity , yes",Toast.LENGTH_SHORT).show();

}

@Override
public void buttonNoClicked() {
    Toast.makeText(MainActivity.this, "I am acticity , No",Toast.LENGTH_SHORT).show();

}


public void showdialog3(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("dialog 3");

    final View v =  getLayoutInflater().inflate(R.layout.dialoglayout,null);
    builder.setView(v);

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            EditText editText_username = v.findViewById(R.id.username);

            Toast.makeText(MainActivity.this, "username:"+ editText_username.getText(),Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show();
        }
    });

    builder.show();

}

} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 MyDialog

package cn.edu.sicnu.dialogdemo;

import android.annotation.SuppressLint; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.widget.Toast;

public class MyDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("dialog 1");
    builder.setMessage("please choose yes or no?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            MyInterface myInterface = (MyInterface)getActivity();
            myInterface.buttonYesClicked();

        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            MyInterface myInterface = (MyInterface)getActivity();
            myInterface.buttonNoClicked();
        }
    });


    return builder.create();
}

} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 MyInterface

package cn.edu.sicnu.dialogdemo;

public interface MyInterface { public void buttonYesClicked(); public void buttonNoClicked();

} 1 2 3 4 5 6 7 8 activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="cn.edu.sicnu.dialogdemo.MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:onClick="showdialog1"
    android:text="Show Dialog"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="showdialog2"
    android:text="Show MyDialog"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="showdialog3"
    android:text="Show Login"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button3" />

</android.support.constraint.ConstraintLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 dialoglayout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:hint="username?"
    android:text="" />
<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="password?"
    android:inputType="textPassword" />

</LinearLayout>