Android編程自定義Dialog的方法分析
本文實例講述了Android編程自定義Dialog的方法。分享給大家供大家參考,具體如下:
功能:
android 提供給我們的只有2種Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特點:1. 不是 Activity 2. 開銷比 Activity 小得多
鑒于以上的優(yōu)點 我們就有定制自己Dialog 的需求
原理:
1. android 系統(tǒng)提供了一個class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)
" 在里面先調(diào)用系統(tǒng)的"super.onCreate(savedInstanceState)
" 其就會保證調(diào)用這個method.
2. 至于 Dialog 界面的定制 可以寫一個xml 文件 然后 在 "void onCreate(Bundle)
" 通過 "setContentView()
" 來使之生效
3. Dialog 使用問題: 1. 彈出:show()
2. 取消:dismiss()
代碼:
1. 創(chuàng)建一個 Dialog:
public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); // TODO Auto-generated constructor stub } protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.custom_dialog); setTitle("Custom Dialog"); TextView text = (TextView)findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView)findViewById(R.id.image); image.setImageResource(R.drawable.sepurple); Button buttonYes = (Button) findViewById(R.id.button_yes); buttonYes.setHeight(5); buttonYes.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); Button buttonNo = (Button) findViewById(R.id.button_no); buttonNo.setSingleLine(true); buttonNo.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); } //called when this dialog is dismissed protected void onStop() { Log.d("TAG","+++++++++++++++++++++++++++"); } }
2. Dialog 的使用:
public class CustomDialogUsage extends Activity { CustomDialog cd; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cd = new CustomDialog(this); Button buttonYes = (Button) findViewById(R.id.main_button); buttonYes.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub cd.show(); } }); } }
3. Dialog 的界面定制:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <Button android:id="@+id/button_yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Yes " android:gravity="center" /> <Button android:id="@+id/button_no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" No " android:gravity="center" /> </LinearLayout> </LinearLayout> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android自定義dialog可選擇展示年月日時間選擇欄
- Android中用Builder模式自定義Dialog的方法
- Android自定義Dialog實現(xiàn)文字動態(tài)加載效果
- Android 自定義Dialog 實例
- Android UI設(shè)計系列之自定義Dialog實現(xiàn)各種風(fēng)格的對話框效果(7)
- Android中制作自定義dialog對話框的實例分享
- Android自定義dialog簡單實現(xiàn)方法
- Android編程經(jīng)典代碼集錦(復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等)
- Android編程中自定義dialog用法實例
- Android 去掉自定義dialog的白色邊框的簡單方法
- Android 自定義dialog的實現(xiàn)代碼
相關(guān)文章
Android編程之SurfaceView學(xué)習(xí)示例詳解
這篇文章主要介紹了Android編程之SurfaceView學(xué)習(xí)示例,結(jié)合實例分析了SurfaceView的功能、使用方法與注意事項,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10android編程判斷應(yīng)用是否具有某個權(quán)限的方法
這篇文章主要介紹了android編程判斷應(yīng)用是否具有某個權(quán)限的方法,涉及Android進程操作及權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10Android實現(xiàn)滑動折疊Header全流程詳解
這篇文章主要介紹了Android實現(xiàn)滑動折疊Header,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11Android 解決ScrollView嵌套CridView顯示問題
這篇文章主要介紹了Android 解決ScrollView嵌套CridView顯示問題的相關(guān)資料,使用ScrollView嵌套CridView的時候會出現(xiàn)顯示不全的問題,這里提供解決辦法,需要的朋友可以參考下2017-08-08Android 倒計時控件 CountDownView的實例代碼詳解
這篇文章主要介紹了Android 倒計時控件 CountDownView的實例代碼,代碼簡單易懂,非常不錯,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android?kotlin?跳轉(zhuǎn)手機熱點開關(guān)頁面和判斷熱點是否打開(親測可用)
跳轉(zhuǎn)手機熱點的頁面肯定是用intent,重點是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,本文介紹Android?kotlin?跳轉(zhuǎn)手機熱點開關(guān)頁面和判斷熱點是否打開,感興趣的朋友一起看看吧2023-08-08Android開發(fā)之使用SQLite存儲數(shù)據(jù)的方法分析
這篇文章主要介紹了Android開發(fā)之使用SQLite存儲數(shù)據(jù)的方法,結(jié)合實例形式分析了Android使用SQLite數(shù)據(jù)庫實現(xiàn)針對數(shù)據(jù)的增刪改查操作相關(guān)技巧,需要的朋友可以參考下2017-07-07