Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android AlertDialog的簡單使用實例

android AlertDialog的簡單使用實例

編輯:關於Android編程

當你的應用需要顯示一個進度條或需要用戶對信息進行確認時,可以使用alertDialog來完成。下面來介紹常用的四種AlertDialog。

1、普通對話框

package com.example.yk.dialogtest; 
 
import android.content.DialogInterface; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
 
/** 
 * AlertDialog普通對話框 
 */ 
public class GeneralDialogActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_general_dialog); 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) 
        .setTitle("操作title")//設置title 
        .setMessage("操作message")//設置要顯示的message 
        .setCancelable(false)//表示點擊dialog其它部分不能取消(除了“取消”,“確定”按鈕) 
        .setPositiveButton("確定", new 
            DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
        Toast.makeText(GeneralDialogActivity.this, "點擊了確定", Toast.LENGTH_SHORT).show(); 
 
      } 
    }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
//        dialogInterface.dismiss(); 
      } 
    }); 
    alertDialog.show();//別忘了show 
  } 
} 

2、單選對話框

package com.example.yk.dialogtest; 
 
import android.content.DialogInterface; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
 
/** 
 * 單選對話框 
 */ 
public class SingleDialogActivity extends AppCompatActivity { 
  private String[] items={"java","php","c"}; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_single_dialog); 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) 
        .setTitle("提示title") 
//        .setMessage("提示message")//在需要設置單選對話框的情況下是不能設置message的,否則單選對話框內容會失效 
        .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {//checkedItem=-1表示默認不選中 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
            Toast.makeText(SingleDialogActivity.this, "選中了"+items[i], Toast.LENGTH_SHORT).show(); 
          } 
        }).setPositiveButton("確定", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
 
          } 
        }); 
      alertDialog.show(); 
  } 
} 

3、多選對話框

package com.example.yk.dialogtest; 
 
import android.content.DialogInterface; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Toast; 
 
/** 
 * 多選對話框 
 */ 
public class MultiChoiceDialogActivity extends AppCompatActivity { 
  private String[] items={"java","php","c"}; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_multi_choice_dialog); 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) 
        .setTitle("提示title") 
        .setCancelable(false) 
        .setMultiChoiceItems(items, new boolean[]{false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { 
 
 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i, boolean b) { 
            if(b){ 
              Toast.makeText(MultiChoiceDialogActivity.this, "選中了"+items[i], Toast.LENGTH_SHORT) 
                  .show(); 
            } 
 
          } 
        }) 
        .setPositiveButton("確定", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
 
          } 
        }); 
    alertDialog.show(); 
  } 
} 

4、進度條對話框

package com.example.yk.dialogtest; 
 
import android.app.ProgressDialog; 
import android.os.Handler; 
import android.os.Message; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
 
/** 
 * 進度條對話框 
 */ 
public class ProgressDialogActivity extends AppCompatActivity { 
  private ProgressDialog progressDialog; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_progress_dialog); 
    progressDialog = new ProgressDialog(this); 
    progressDialog.setTitle("提示title"); 
    progressDialog.setCancelable(true); 
//    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//旋轉進度條,默認風格 
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//橫向進度條 
    progressDialog.show(); 
 
  } 
} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved