Android studio Dialog 弹出式对话框

文件结构

这里写图片描述

运行效果

这里写图片描述

主要代码

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();

    }

}

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();
    }
}

MyInterface

package cn.edu.sicnu.dialogdemo;

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

}

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>

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>

猜你喜欢

转载自blog.csdn.net/haolangtaiye/article/details/80309724