[安卓筆記]盤點android中的dialog

除瞭Toast和Notification之外,Dialog也可用於提示用戶。 創建dialog需要用到AlertDialog.Builder類,這可以理解為一個對話框的產生器,可以利用這個類創建各種種類的對話框。對話框創建好之後調用此類的create方法即可產生一個對話框,再調用show方法就可以將對話框顯示到界面上。 如果對話框用來展示一個進度,可以使用ProgressDialog類來完成(不建議使用,應該用ProgressBar代替)。另外還有兩類對話框一個叫DatePickerDialog另一個是TimePickerDialog分別用來選擇日期和時間。 除此之外,對話框可以使用DialogFrgament類來包裝,但是為瞭兼容性需要導入v4的包。具體可以看文檔。 下面以代碼片段的方式總結瞭一些常用的dialog的使用: 1.最基本的dialog: 顯示效果:
代碼片段:

public void show_alert()
    {
        AlertDialog.Builder builder = new Builder(this);
        builder.setMessage("是否更新");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("提醒");
        builder.setPositiveButton("確定",new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this,"點擊瞭確定按鈕",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this,"點擊瞭取消按鈕",Toast.LENGTH_SHORT).show();
//                dialog.dismiss();
            }
        });
        builder.show();
    }

2.帶一個列表的對話框:1).列表數據靜態指定(放到數組中): 顯示效果:
代碼片段:

private void show_list()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("職業");
        builder.setItems(new String[]{"程序員","工程師","磚傢"},new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this,"點擊瞭"+which,Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

2).列表數據通過適配器指定 顯示效果:
代碼片段:

public void show_dialog()
    {
        /* 這裡的數據可以從數據庫/網絡中動態獲取 */
        List<map> data = new ArrayList<map>();
        Map map1 = new HashMap();
        map1.put("image", R.drawable.image1);
        map1.put("text", "我是條目1");
        Map map2 = new HashMap();
        map2.put("image", R.drawable.image2);
        map2.put("text", "我是條目2");
        Map map3 = new HashMap();
        map3.put("image", R.drawable.image3);
        map3.put("text", "我是條目3");
        data.add(map1);
        data.add(map2);
        data.add(map3);
        SimpleAdapter adapter = new SimpleAdapter(this, data,
                R.layout.list_item, new String[]
                { "image", "text" }, new int[]
                { R.id.iv_show, R.id.tv_text });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("從適配器中獲取的數據");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this, "點擊瞭" + which, 0).show();
            }
        });
        builder.show();
    }</map</map

3.單選列表對話框 顯示效果:

代碼片段:

public void show_single()
    {
        AlertDialog.Builder builder = new Builder(this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("提醒");
        builder.setSingleChoiceItems(new String[]{"條目1","條目2","條目3"},0,new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)//id從0開始
            {
                Toast.makeText(MainActivity.this,"item="+which,Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this,"點擊瞭取消按鈕",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

4.多選列表對話框: 顯示效果:

代碼片段:

public void show_multiple()
    {
        AlertDialog.Builder builder = new Builder(this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("提醒");
        builder.setMultiChoiceItems(new String[]{"條目1","條目2","條目3","條目4"}, null, new DialogInterface.OnMultiChoiceClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked)
            {
                Toast.makeText(MainActivity.this,"item"+which+","+isChecked,Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this,"點擊瞭取消按鈕",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

5.自定義對話框: 顯示效果:

代碼片段:

private void show_own_dialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();//"充氣筒"
        builder.setView(inflater.inflate(R.layout.dialog, null))
                .setPositiveButton("登陸",
                        new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                Toast.makeText(MainActivity.this, "確定", 0).show();
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                Toast.makeText(MainActivity.this,"取消", 0).show();
                            }
                        });
        builder.show();
    }

6.帶進度條的dialog 顯示效果:
代碼片段:

public void show_circle_progressdialog()
    {
        ProgressDialog diaolog = new ProgressDialog(this);
        diaolog.setIcon(R.drawable.ic_launcher);
        diaolog.setTitle("提醒");
        diaolog.setMessage("正在加載");
        diaolog.show();
    }

還有水平進度條: 顯示效果:

代碼片段:

public void show_horizonal()
    {
        final ProgressDialog diaolog = new ProgressDialog(this);
        diaolog.setIcon(R.drawable.ic_launcher);
        diaolog.setTitle("提醒");
        diaolog.setMessage("正在加載");
        diaolog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        diaolog.setMax(100);
        
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for(int i = 0; i<=100; i++)
                {
                    diaolog.setProgress(i);
                    try
                    {
                        Thread.sleep(30);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                diaolog.dismiss();
            }
        }).start();
        diaolog.show();
    }

7.日期選擇與時間選擇對話框: 顯示效果:

代碼片段: 佈局:


    
      

activity:

package com.example.dialogdemo4;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class MainActivity extends Activity
{
    private DatePicker dp = null;
    private TimePicker tp = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        dp = (DatePicker) findViewById(R.id.dp);
        tp = (TimePicker) findViewById(R.id.tp);
        
        dp.init(2014, 1, 1, new OnDateChangedListener()
        {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth)
            {
                Toast.makeText(MainActivity.this,"monthOfYear"+monthOfYear+",dayOfMonth"+dayOfMonth, 0).show();
            }
        });
        
        tp.setIs24HourView(true);
        tp.setOnTimeChangedListener(new OnTimeChangedListener()
        {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
            {
                Toast.makeText(MainActivity.this,"hourOfDay"+hourOfDay+",minute"+minute, 0).show();
            }
        });      
    }
}

——————————————————–完——————————————————–
轉載請註明出處,謝謝! ——————————————————————————————————————

發佈留言