手機APP對話框是不少APP都有的下面來看下怎麼實現的吧,android
打開Android studio 然他自動建立好佈局和類;app
下面咱們修改activity_main.xml中的代碼ide
<?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中的代碼ui
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();來去取消關閉對話框;this
下面附上第一個和第二個效果圖.xml