AndroidStudio对话框详解+完整代码

AndroidStudio对话框详解+完整代码

first one:普通对话框

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

import android.os.Bundle;


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlertDialog ad = new AlertDialog.Builder(this)
                .setTitle("普通对话框")
                .setMessage("是否确定退出")
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("confirm",null)
                .setNegativeButton("exit",null)
                .create();
        ad.show();
    }
}

AndroidStudio对话框详解+完整代码_第1张图片

second:单选对话框


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

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


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       new AlertDialog.Builder(this)
               .setTitle("singleChoice")
               .setSingleChoiceItems(new String[]{"男", "女"}, 0, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       
                   }
               })
               .setIcon(R.mipmap.ic_launcher)
               .setPositiveButton("CONFIRM",null)
               .show();
    }
}

效果图
AndroidStudio对话框详解+完整代码_第2张图片

third:多选对话框


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

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


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new AlertDialog.Builder(this)
                .setTitle("MultiChoiceItem")
                .setIcon(R.mipmap.ic_launcher)
                .setMultiChoiceItems(new String[]{"one", "two", "three", "four"}, new boolean[]{true, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                })
                .setPositiveButton("confirm",null)
                .show();
    }



}

效果图
AndroidStudio对话框详解+完整代码_第3张图片

fourth:进度条

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

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProgressDialog pd = new ProgressDialog(this);
        pd.setIcon(R.mipmap.ic_launcher);
        pd.setTitle("ProgressDialog");
        pd.setMessage("loading...");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
    }



}

效果图
AndroidStudio对话框详解+完整代码_第4张图片

fifth:Toast

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity{

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

        Toast.makeText(this,"finished",Toast.LENGTH_LONG).show();
    }



}

效果图
AndroidStudio对话框详解+完整代码_第5张图片
sixth:自定义对话框
步骤:
one:创建布局,写出对话框的xml布局文件
two:初始化自定义对话框中的控件以及响应按钮的点击事件
three:在mainActivity.java中调用自定义类的构造方法,显示对话框

你可能感兴趣的:(android,移动开发,安卓)