Android開發中,dialog的實現方式
阿新 • • 發佈:2019-01-04
在Android開發中,我們經常會需要在Android介面上彈出一些對話方塊,比如詢問使用者或者讓使用者選擇。這些功能我們叫它Android Dialog對話方塊,在我們使用Android的過程中,我歸納了一下,Android Dialog的型別無非也就7種,下面我分別向大家介紹這7種Android Dialog對話方塊的使用方法,希望對大家能有所幫助。
1.該效果是當按返回按鈕時彈出一個提示,來確保無誤操作,採用常見的對話方塊樣式。
建立dialog對話方塊方法程式碼如下:
- protectedvoid dialog() {
- AlertDialog.Builder builder = new
- builder.setMessage("確認退出嗎?");
- builder.setTitle("提示");
- builder.setPositiveButton("確認", new OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Main.this.finish();
- }
- });
- builder.setNegativeButton("取消"
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
在onKeyDown(int keyCode, KeyEvent event)方法中呼叫此方法
- publicboolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==
- dialog();
- }
- returnfalse;
- }
2..改變了對話方塊的圖表,添加了三個按鈕
建立dialog的方法程式碼如下:
- Dialog dialog = new AlertDialog.Builder(this).setIcon(
- android.R.drawable.btn_star).setTitle("喜好調查").setMessage(
- "你喜歡李連杰的電影嗎?").setPositiveButton("很喜歡",
- new OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "我很喜歡他的電影。",
- Toast.LENGTH_LONG).show();
- }
- }).setNegativeButton("不喜歡", new OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "我不喜歡他的電影。", Toast.LENGTH_LONG)
- .show();
- }
- }).setNeutralButton("一般", new OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(Main.this, "談不上喜歡不喜歡。", Toast.LENGTH_LONG)
- .show();
- }
- }).create();
- dialog.show();
3.資訊內容是一個簡單的View型別
建立dialog方法的程式碼如下:
- new AlertDialog.Builder(this).setTitle("請輸入").setIcon(
- android.R.drawable.ic_dialog_info).setView(
- new EditText(this)).setPositiveButton("確定", null)
- .setNegativeButton("取消", null).show();
4.資訊內容是一組單選框
建立dialog方法的程式碼如下:
- new AlertDialog.Builder(this).setTitle("複選框").setMultiChoiceItems(
- new String[] { "Item1", "Item2" }, null, null)
- .setPositiveButton("確定", null)
- .setNegativeButton("取消", null).show();
5.資訊內容是一組多選框
建立dialog方法的程式碼如下:
- new AlertDialog.Builder(this).setTitle("單選框").setIcon(
- android.R.drawable.ic_dialog_info).setSingleChoiceItems(
- new String[] { "Item1", "Item2" }, 0,
- new DialogInterface.OnClickListener() {
- publicvoid onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- }).setNegativeButton("取消", null).show();
6.資訊內容是一組簡單列表項
建立dialog的方法程式碼如下:
- new AlertDialog.Builder(this).setTitle("列表框").setItems(
- new String[] { "Item1", "Item2" }, null).setNegativeButton(
- "確定", null).show();
7.資訊內容是一個自定義的佈局
dialog佈局檔案程式碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content" android:layout_width="wrap_content"
- android:background="#ffffffff" android:orientation="horizontal"
- android:id="@+id/dialog">
- <TextView android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:id="@+id/tvname" android:text="姓名:" />
- <EditText android:layout_height="wrap_content"
- android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
- </LinearLayout>
建立dialog方法的程式碼如下:
- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.dialog,
- (ViewGroup) findViewById(R.id.dialog));
- new AlertDialog.Builder(this).setTitle("自定義佈局").setView(layout)
- .setPositiveButton("確定", null)
- .setNegativeButton("取消", null).show();
好了,以上7種Android dialog對話方塊的使用方法就介紹到這裡了,基本都全了