1. 程式人生 > >Android 4 學習(21):對話方塊

Android 4 學習(21):對話方塊

對話方塊

建立Dialog的兩種方式:

1. 使用Dialog類或其子類,包括DialogFragment

2. Activity中使用Dialog主題(theme

下面是使用Dialog類的一個例子:

// Create the new Dialog.
Dialog dialog = new Dialog(MyActivity.this);
// Set the title. dialog.setTitle(“Dialog Title”); // Inflate the layout. dialog.setContentView(R.layout.dialog_view);
// Update the Dialog’s contents.
TextView text = (TextView)dialog.findViewById(R.id.dialog_text_view);
text.setText(“This is the text in my dialog”);
// Display the Dialog. dialog.show();

也可以使用Dialog的子類,例如常用的AlertDialog

Context context = MyActivity.this;
String title = “It is Pitch Black”;
String message = “You are likely to be eaten by a Grue.”;
String button1String = “Go Back”;
String button2String = “Move Forward”; AlertDialog.Builder ad
= new AlertDialog.Builder(context);
ad.setTitle(title);ad.setMessage(message); ad.setPositiveButton(button1String,
new DialogInterface.OnClickListener() {   public void onClick(DialogInterface dialog, int arg1) {     eatenByGrue();   } }); ad.setNegativeButton(button2String, new DialogInterface.OnClickListener(){   
public void onClick(DialogInterface dialog, int arg1) {     //do nothing   } });

還可以設定是否顯示Cancel按鈕:

ad.setCancelable(true); 
ad.setOnCancelListener(new DialogInterface.OnCancelListener() { 
  public void onCancel(DialogInterface dialog) { 
    eatenByGrue();   }
});

使用內建的Dialog

Android中提供了這些現成的Dialog供我們使用:

1. CharacterPickerDialog 

2. DatePickerDialog 

3. TimePickerDialog 

4. ProgressDialog 

使用Fragment

Android中幾乎所有的可見部分都可以放到Fragment中,我認為Fragment相對於View的一大優點是可以控制自己的生命週期。

public class MyDialogFragment extends DialogFragment {
  private static String CURRENT_TIME = “CURRENT_TIME”; 
  public static MyDialogFragment newInstance(String currentTime) {
    // Create a new Fragment instance with the specified parameters.
    MyDialogFragment fragment = new MyDialogFragment();
    Bundle args = new Bundle();     args.putString(CURRENT_TIME, currentTime);     fragment.setArguments(args);     return fragment;   }
}

相關推薦

Android 4 學習21對話方塊

對話方塊 建立Dialog的兩種方式: 1. 使用Dialog類或其子類,包括DialogFragment 2. 在Activity中使用Dialog主題(theme) 下面是使用Dialog類的一個例子: // Create the new Dialog.Dialog dialog = n

Android 4 學習18搜尋

參考《Professional Android 4 Development》 搜尋 通過下面這幾種方式可以給應用程式新增搜尋功能: Search Bar Search View Quick Search Box 可搜尋的Content Provider 首

Android 4 學習17使用Content Resolver

Content Resolver簡介 每個應用程式都有一個ContentResolver例項,通過getContentResolver()方法可以獲取: ContentResolver cr = getContentResolver(); 與Content Provider對應,Cont

Android 4 學習20ActionBar

參考《Pro Android 4.0》 ActionBar 11.0之後,ActionBar在Activity中預設存在,可以在程式碼中設定其顯示與否: ActionBar actionBar = getActionBar(); // Hide the Action Bar actionBa

Android 4 學習19Services

參考《Professional Android 4 Development》 Services Service是invisible的,因此其優先順序不高於visible的Activity,之所以說不高於,是因為我們可以設定Service為在前臺執行。 建立Service Android提供了Ser

Android 4學習7使用者介面

參考《Professional Android 4 Development》 Android UI基本元素 下面這些概念是Android UI設計的基礎,深入學習和理解它們是Android UI設計的基礎: View:View是所有UI元素,包括Layout在內,的父

Android 4學習6概述

參考:《Professional Android 4 Application Development》 深入瞭解Android Activity 每一個Android Activity都對應於一個使用者介面(UI)。每個Android Application都有一個m

Android NDK學習編譯腳本語法Android.mk和Application.mk

GC make files 文件的 包括 一次 opengl aries 基本語法 一、Android.mk Android.mk分為一下幾部分: LOCAL_PATH:= $(call my-dir), 返回當前文件在系統中的路徑,Android.mk文件開始時必須定義

Android Camera學習如何實現轉動螢幕介面選單跟著轉動效果

最近公司在做車載專案,需要把照相機原本豎向顯示改為橫向顯示。所以研究了下camera選單朝向的問題。 系統提供了一個監聽sensor狀態變化的類OrientationEventListener。在系統程式碼CameraActivity中就是繼承的這個類。 private

Android BLE學習編寫自己的 BLE藍芽讀寫工具功能仿照nrf master control panel

背景 由於nordic官方的nrf master control panel只提供了apk,很多同學學習起來都得自己摸索藍芽的讀寫,專案中整理了BLE模組的基本讀寫方法以及一些常用的UUID,並且抽取了一些藍芽操作的流程,方便Android app程式碼開發,

Android BLE學習 Android搜尋BLE裝置

背景 總結一下最近ble的學習情況。自從入手ble 51822開發板後就開始不停加班,中途出於好奇,業餘時間寫了一些微控制器上json解析相關的東西,妄圖使用藍芽傳輸json資料,不知道是否實用,既然開始寫了,得寫出點樣子,晃晃蕩蕩,2016年的1月份就過去了

深入學習Gremlin21範圍

第21期 Gremlin Steps: local()、count()、max()、mean()、min()、sum()、order()、tail()、limit()、range()、sample()、skip()、limit()、dedup() 本系列文章的G

Android學習21 -- Android版本號和API Level對應關係

Platform Version API Level VERSION_CODE Android 6.0 23 API Level Marshmallow Android 5

linux命令學習6ps命令

bytes 釋放 ice cti width kthread hellip 名稱 pts Linux中的ps命令是Process Status的縮寫。ps命令用來列出系統中當前運行的那些進程。ps命令列出的是當前那些進程的快照,就是執行ps命令的那個時刻的那些進程,如果想要

JAVA學習方法重載與方法重寫、thiskeyword和superkeyword

格式 hello new 初始 per 而且 方法重寫 學習 方式 方法重載與方法重寫、thiskeyword和superkeyword 1、方法重載 重載可以使具有同樣名稱但不同數目和類型參數的類傳遞給方法。 註: 一是重載方法的參數列表必須與被重載的方法不同

Linux命令學習17ifconfig命令

廣播 參考 vip 統計 協議 cnblogs 還需要 pro 網絡 版權聲明更新:2017-05-22博主:LuckyAlan聯系:[email protected]/* */聲明:吃水不忘挖井人,轉載請註明出處! 1 文章介紹 我們知道,在windows中,

ArcGIS API for JavaScript學習1第一個地圖

樣式表 參數 資源 charset 底層 arcgis 順序 api navi 1.簡介 ArcGIS API for JavaScript跟隨ArcGIS 9.3同時發布,是ESRI根據JavaScript技術實現的調用ArcGIS Server REST API接口的一

Java學習2將鍵盤錄入的內容保存到指定文件中

stream exce 創建 txt 關閉 如果 下午 line 再次 要求:保存鍵盤錄入的內容,當鍵盤輸入end時,錄入結束。 1 /** 2 * 保存鍵盤輸入,並以end結束 3 * 4 * @author xcx 5 * @time 2017年6

RabbitMQ學習遠程結果調用

cells actor ble 隨機 get getenv all 求和 int 場景:我們需要在傳輸消息時得到結果 客服端在發送請求時會發送回調隊列,服務端處理事情完成後會將結果返回到回調隊列中,在增加關聯標誌關聯每個請求和服務返回 客戶端代碼: public

facets學習1什麽是facets

遺失 air 不同的 無縫切換 over 快速 後來 樣式 觀察 ML 數據集可以包含數億個數據點,每個數據點由數百(甚至數千)的特征組成,幾乎不可能以直觀的方式了解整個數據集。為幫助理解、分析和調試 ML 數據集,谷歌開源了 Facets,一款可視化工具。 Facets