Android Dialog對話框詳解
更新時間:2016年03月24日 09:50:51 作者:jerrylsxu
這篇文章主要介紹了Android Dialog對話框詳解的相關資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼了。
布局文件xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".DialogActivity" > <Button android:id="@+id/plainDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通Dialog" /> <Button android:id="@+id/plainDialogEvent" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Dialog按鈕事件集中處理" /> <Button android:id="@+id/inputDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請輸入框" /> <Button android:id="@+id/listDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表對話框" /> <Button android:id="@+id/radioDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="單選對話框" /> <Button android:id="@+id/checkboxDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多選對話框" /> <Button android:id="@+id/diyDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義布局對話框" /> </LinearLayout>
Activity文件:
普通的dialog:
private void plainDialogDemo() { Button plainBtn = (Button) findViewById(R.id.plainDialog); plainBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this) .setTitle("刪除") .setMessage("確定刪除指定數(shù)據(jù)") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "確定了", Toast.LENGTH_SHORT) .show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setCancelable(false).show(); } }); }
效果如下:
輸入文本框的dialog:
private void inputDialog() { Button inputBtn = (Button) findViewById(R.id.inputDialog); inputBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final EditText et = new EditText(DialogActivity.this); new AlertDialog.Builder(DialogActivity.this) .setTitle("請輸入數(shù)字") .setView(et) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), et.getText(), Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", null) .setCancelable(false).show(); } }); }
效果如下:
列表dialog:
private void listDialogDemo() { Button listBtn = (Button) findViewById(R.id.listDialog); listBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String[] names = { "C羅", "J羅", "H羅" }; new AlertDialog.Builder(DialogActivity.this).setTitle("列表對話框") .setItems(names, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogActivity.this, names[which], Toast.LENGTH_SHORT) .show(); } }).setNegativeButton("取消", null).show(); } }); }
效果如下:
單選dialog:
private void radioDialogDemo() { Button radioButton = (Button) findViewById(R.id.radioDialog); radioButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String[] names = { "C羅", "J羅", "H羅" }; new AlertDialog.Builder(DialogActivity.this) .setTitle("列表對話框") .setSingleChoiceItems(names, , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selecteName = names[which]; } }) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogActivity.this, selecteName, Toast.LENGTH_SHORT) .show(); } }).setNegativeButton("取消", null).show(); } }); }
效果如下:
多選dialog:
private void checkDialogDemo() { Button checkBtn = (Button) findViewById(R.id.checkboxDialog); checkBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String[] names = { "C羅", "J羅", "H羅" }; final boolean[] selected = new boolean[] { true, false, true }; new AlertDialog.Builder(DialogActivity.this) .setMultiChoiceItems( names, selected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuilder sb = new StringBuilder( "你選擇了:"); for (int i = ; i < names.length; i++) { if (selected[i]) { sb.append(names[i]); } } Toast.makeText(DialogActivity.this, sb.toString(), ).show(); } }).setNegativeButton("取消", null).show(); } }); }
效果如下:
自定義dialog:
private void customDialogDemo() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.diylayout); ImageButton ok = (ImageButton) window.findViewById(R.id.btnok); ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "關閉了", Toast.LENGTH_SHORT).show(); dlg.dismiss(); } }); }
自定義布局:
<?xml version="." encoding="utf-"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/dialogimg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/dialog_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/dialogimg" android:layout_alignTop="@id/dialogimg" android:layout_marginLeft="dp" android:layout_marginTop="dp" android:text="自定義的dialog" /> <ImageButton android:id="@+id/btnok" android:layout_width="dp" android:layout_height="dp" android:layout_alignRight="@id/dialogimg" android:layout_alignTop="@id/dialogimg" android:layout_marginRight="dp" android:layout_marginTop="dp" android:background="@drawable/close_dialog" /> </RelativeLayout>
效果如:
有關Android Dialog對話框詳解小編就給大家介紹這么多,希望對大家有所幫助!
您可能感興趣的文章:
- Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
- Android使用自定義alertdialog實現(xiàn)確認退出按鈕
- Android實現(xiàn)點擊AlertDialog上按鈕時不關閉對話框的方法
- Android中自定義對話框(Dialog)的實例代碼
- 8種android 對話框(Dialog)使用方法詳解
- Android UI設計系列之自定義Dialog實現(xiàn)各種風格的對話框效果(7)
- 屬于自己的Android對話框(Dialog)自定義集合
- Android中制作自定義dialog對話框的實例分享
- Android實現(xiàn)自定義圓角對話框Dialog的示例代碼
- Android開發(fā)之利用Activity實現(xiàn)Dialog對話框
- Android編程實現(xiàn)帶有單選按鈕和復選按鈕的dialog功能示例
相關文章
Android ContentProvider的實現(xiàn)及簡單實例代碼
這篇文章主要介紹了Android ContentProvider的實現(xiàn)及簡單實例代碼的相關資料,需要的朋友可以參考下2017-02-02詳解Android的MVVM框架 - 數(shù)據(jù)綁定
這篇文章主要介紹了詳解Android的MVVM框架 - 數(shù)據(jù)綁定,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡連接狀態(tài)的改變
這篇文章主要為大家詳細介紹了Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡連接狀態(tài)的改變,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05