示例1:简单的AlertDialog

 215036982  

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class MainActivity extends Activity {
                                                 
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {
                                                             
                new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("对话框")
                .setMessage("显示的内容")
                .setPositiveButton("确定"new DialogInterface.OnClickListener() {
                                                                 
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub                    
                    }
                })
                .setNegativeButton("取消"new DialogInterface.OnClickListener() {
                                                                 
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub                    
                    }
                })
                .setNeutralButton("其他"null)//事件监听同上
                .show();              
            }
        });   
    }
}

 

示例2:包含列表的Dialog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MainActivity extends Activity {
    private String[] data ={"列表1","列表1","列表1"};
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {
                                              
                new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("列表对话框")
                .setItems(data, new DialogInterface.OnClickListener() {
                                                  
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub                
                    }
                })
                .setPositiveButton("确定"new DialogInterface.OnClickListener() {
                                                  
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub                    
                    }
                })
                .show();              
            }
        });   
    }
}

 

运行结果:

 

 

示例3:自定义AlertDialog

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MainActivity extends Activity {
    private String[] data ={"列表1","列表1","列表1"};
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {
                LayoutInflater factory=LayoutInflater.from(MainActivity.this);
                final View view=factory.inflate(R.layout.cell, null);
                             
                new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("自定义对话框")
                .setView(view)
                .setPositiveButton("确定"new DialogInterface.OnClickListener() {
                                 
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub                    
                    }
                })
                .show();              
            }
        });   
    }
}

运行结果:

 

本文出自 “天马行空” 博客,请务必保留此出处http://shaotao.blog.51cto.com/7606729/1288801

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()