Dialong弹窗与PopWindow弹窗

来源:互联网 发布:安工大网络接入系统 编辑:程序博客网 时间:2024/06/03 12:27
public class MyActivity extends Activity {
//创建按钮启动弹窗的按钮
    private Button mbutton,mbutton1;
//PopWindow弹窗内容
    private String[] array=new String[]{"张三","李四","王二","麻子"};
//创建PopWindow变量
    private PopupWindow popupWindow;
//创建Dialog变量
    AlertDialog dialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//找到Dialog启动按钮并监听
        mbutton=(Button)findViewById(R.id.button);
        mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//弹出这个对话框
            showDialog(1);
            }
        });
//将当前对象转成 LayoutInflater对象
        LayoutInflater layoutInflater=LayoutInflater.from(this);
//找到 PopWindow布局XML转成View对象
        View v=layoutInflater.inflate(R.layout.popwindow_item_list,null);
//用ListView对象接收XML文件中的ListView
        ListView listView=(ListView)v.findViewById(R.id.onetextview);
//因为只有字符,使用ArrayAdapter放入array数组的值
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
//为这个listView放入这个适配器
        listView.setAdapter(arrayAdapter);
//找到启动PopWindow的启动按钮并监听
        mbutton1=(Button)findViewById(R.id.button1);
//new一个PopWindow并放入v初iew值和始化弹窗大小
        popupWindow=new PopupWindow(v,200,400);
        mbutton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else {
                    popupWindow.showAsDropDown(mbutton1);
                }
            }
        });
    }
    protected Dialog onCreateDialog(int id) {
//判断ID
        switch (id){
//当id是0的时候弹出这个
        case 0:AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("aa");
        builder.setMessage("bb");
         dialog=builder.create();
        return dialog;
//当id是1弹出这个
            case 1:MyDialog dialog1=new MyDialog(MyActivity.this);
                return dialog1;
    }
        return null;
    }
}
0 0