Android中的AlertDialog和ProgressDialog用法


 

手機APP對話框是很多APP都有的下面來看下怎么實現的吧,

打開Android studio 然他自動創建好布局和類;

下面我們修改activity_main.xml中的代碼

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

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_gravity="center"
        android:layout_marginTop="60pt"
        android:textAllCaps="false"
        />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button"
        android:layout_marginTop="3pt"
        android:textAllCaps="false"
        />

</LinearLayout>

  再activity_main.xml添加了兩個按鈕 分別是用於 AlertDialog  和 ProgressDialog的

  下面我們再看ActivityMain中的代碼

  

package com.example.administrator.myappalertdialog;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBtn1;
    private Button mBtn2;  //聲明控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionbar = getSupportActionBar();  // 去除標題欄
        if(actionbar!= null)
        {
            actionbar.hide();
        }
        mBtn1 = (Button) findViewById(R.id.btn1);  //查找控件
        mBtn1.setOnClickListener(this);   //創建點擊事件
        mBtn2 = (Button) findViewById(R.id.btn2);
        mBtn2.setOnClickListener(this);
    }

    public void onClick(View view)  //點擊事件
    {
        switch(view.getId())
        {
            case R.id.btn1:AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("This is AlertDialog");  //對話框標題
                dialog.setMessage("Hello world!");  //內容
                dialog.setCancelable(true);    //可撤銷性
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_SHORT).show();  //提示內容
                    }   //確定按鈕的點擊事件
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();  //提示內容
                    }//取消按鈕的點擊事件
                });
                dialog.show();  //全部顯示出來
                break;
//下面的原理一樣。
            case R.id.btn2:
                ProgressDialog progressdialog = new ProgressDialog(MainActivity.this);
                progressdialog.setTitle("This is ProgressDialog");
                progressdialog .setMessage("Mr.Jiang");
                progressdialog.setCancelable(false);
                progressdialog.show();
                break;

            default: Toast.makeText(MainActivity.this,"ERROR",Toast.LENGTH_SHORT).show();break;
        }
    }
}

  上面ProgressDialog 沒有確定和取消按鈕,我們可以把progressdialog.setCancelable里面的值改成true,按下BACK鍵即可退出,還有可以用progressdialog.dismiss();來去取消關閉對話框;

    下面附上第一個和第二個效果圖.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM