Android Studio常用对话框

下面我们一起来了解一下对话框,对话框是我们以后Android必要的一些按键。下面是我整理的三种常用对话框。
如图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

请看代码部分:
1.MainActivity.java(主界面)
package cn.qjnu.wxf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button button;  //创建一个按钮

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

    button = findViewById(R.id.bt_one);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ProgressDialog prodgressdialog;
            prodgressdialog = new ProgressDialog(MainActivity.this);
            prodgressdialog.setTitle("下载进度");
            prodgressdialog.setIcon(R.mipmap.ic_launcher);
            prodgressdialog.setMessage("正在下载...");
            prodgressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            prodgressdialog.show();
        }
    });
}

//普通对话框
public void click1(View view) {
    AlertDialog dialog;
    dialog = new AlertDialog.Builder(this)
            .setTitle("普通对话框")  //设置标题
            .setIcon(R.mipmap.ic_launcher) //设置图标
            .setMessage("确定退出?") //提示信息
            .setPositiveButton("确定",null)   //添加“确定”按钮
            .setNegativeButton("取消",null)   //添加“取消”按钮
            .create();  //创建对话框
    dialog.show();  //显示对话框
}

//单选对话框
public void click2(View view) {
    AlertDialog dialog;
    dialog = new AlertDialog.Builder(this)
            .setTitle("选择性别")  //设置标题
            .setIcon(R.mipmap.ic_launcher) //设置图标
            .setSingleChoiceItems(new String[]{"男","女"}, 0,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }
            )
            .setPositiveButton("确定",null)   //添加“确定”按钮
            .setNegativeButton("取消",null)   //添加“取消”按钮
            .create();  //创建对话框
    dialog.show();  //显示对话框
}

//多选对话框
public void click3(View view) {
    AlertDialog dialog;
    dialog = new AlertDialog.Builder(this)
            .setTitle("选择你的兴趣")  //设置标题
            .setIcon(R.mipmap.ic_launcher) //设置图标
            .setMultiChoiceItems(new String[]{"看电影","打游戏","旅游","吃","喝"},null,null)
            .setPositiveButton("确定",null)   //添加“确定”按钮
            .create();  //创建对话框
    dialog.show();  //显示对话框
}

//圆形进度条
public void click4(View view) {
    ProgressDialog prodgressdialog;
    prodgressdialog = new ProgressDialog(MainActivity.this);
    prodgressdialog.setTitle("下载进度");
    prodgressdialog.setIcon(R.mipmap.ic_launcher);
    prodgressdialog.setMessage("正在下载...");
    prodgressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    prodgressdialog.show();
}

//消息对话框
public void click5(View view) {
    Toast.makeText(this,"我是消息对话框",Toast.LENGTH_SHORT).show();
}

//样式
public void click7(View view) {
    Intent intent =new Intent(this,styleActivity.class);
    startActivity(intent);
}

//主题
public void click8(View view) {
    Intent intent = new Intent(this,themeActivity.class);
    startActivity(intent);
}


//自定义对话框
public void click6(View view) {
    MyDialog myDialog = new MyDialog(this,"hello!我是你的自定义");

    myDialog.show();
}

}

1.activity_main.xml(主界面)

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_margin="10dp"
    android:text="常用对话框案例"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click1"
    android:text="普通对话框"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click2"
    android:text="单选对话框"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click3"
    android:text="多选对话框"/>

<Button
    android:id="@+id/bt_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="水平进度条"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click4"
    android:text="圆形进度条"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click5"
    android:text="消息对话框"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click6"
    android:text="自定义对话框"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click7"
    android:text="样式"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="click8"
    android:text="主题"/>

2.activity_my_dialog.xml(自定义对话框)

      <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    android:orientation="vertical"
    tools:context=".MyDialog">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="40sp"
            android:background="#0080FF"
            android:gravity="center"
            android:text="自定义对话框"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            android:visibility="visible" />

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <TextView
                android:id="@+id/tv_msg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:minHeight="100dp"
                android:paddingBottom="15dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="15dp"
                android:textColor="#ff6666"
                android:textSize="16sp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="bottom"
            android:background="#E0E0E0"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_ok"
                android:layout_width="114dp"
                android:layout_height="40dp"
                android:layout_marginLeft="20dp"
                android:background="#FF8000"
                android:gravity="center"
                android:text="确定"
                android:textColor="#ffffff"
                android:textSize="15dp"/>

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="114dp"
                android:layout_height="40dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="#d0d0d0"
                android:gravity="center"
                android:text="取消"
                android:textColor="#666666"
                android:textSize="15sp"/>
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

2.MyDialog.java(自定义对话框)
package cn.qjnu.wxf;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;


public class MyDialog extends Dialog {
    private String dialogName;
    private TextView tvMsg;
    private Button btnOK;
    private  Button btnCancel;
    public MyDialog(Context context, String dialogName){
        super(context);
        this.dialogName = dialogName;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //去除标题
        setContentView(R.layout.activity_my_dialog);
        tvMsg = (TextView) findViewById(R.id.tv_msg);
        btnOK = (Button) findViewById(R.id.btn_ok);
        btnCancel = (Button) findViewById(R.id.btn_cancel);
        tvMsg.setText(dialogName);  //设置自定义对话框显示内容

        //为“确定”按钮设置点击事件
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此处写需要处理的逻辑
            }
        });

        //为“取消”按钮设置点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();  //关闭当前对话框
            }
        });

    }
}

3.activity_style.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"
    android:orientation="vertical"
    tools:context=".styleActivity">

    <TextView
        style="@style/textStyle_one"
        android:text="样式一" />

    <TextView
        style="@style/textStyle_two"
        android:text="样式二" />

</LinearLayout>

3.styleActivity.java(样式)
package cn.qjnu.wxf;

import android.os.Bundle;
import android.app.Activity;

public class styleActivity extends Activity {

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

}

4.activity_theme.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"
    android:orientation="vertical"
    tools:context=".styleActivity">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:textSize="38sp"
       android:text="主题"/>

</LinearLayout>

**4.themeActivity.java(主题)**
package cn.qjnu.wxf;

import android.os.Bundle;
import android.app.Activity;

public class themeActivity extends Activity {

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

}

6.values–>styles.xml配置

<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!--主题-->
<style name="mySyle" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">#999999</item>
</style>

<!--样式1-->
<style name="textStyle_one">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#aaa251</item>
    <item name="android:textSize">25sp</item>
</style>

<!--样式2-->
<style name="textStyle_two" parent="textStyle_one">
    <item name="android:textSize">35sp</item>
</style>

`

文件目录:
在这里插入图片描述
讲解:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
源码下载:https://download.csdn.net/download/weixin_43849104/11065970
有问题欢迎留言
如果觉得有用记得关注我!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章