一个简单的Dialog实现

来源:互联网 发布:我是神经病网络歌手 编辑:程序博客网 时间:2024/05/20 10:53

publicclass MyDialogDemo extends Activity {

    private ImageButton but = null ;    // 定义按钮

    @Override

    public void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main); // 调用布局管理器

        this.but = (ImageButton)super.findViewById(R.id.but) ; //取得按钮

        this.but.setOnClickListener(newOnClickListenerImpl()) ;    //设置事件类

    }

    private class OnClickListenerImpl implementsOnClickListener {

 

        @Override

        public void onClick(View view) {

            MyDialogDemo.this.exitDialog() ;

        }

       

    }

    @Override

    public boolean onKeyDown(int keyCode,KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) { // 返回键

            this.exitDialog() ;

        }

        return false ;

    }

    private void exitDialog(){

        Dialog dialog = newAlertDialog.Builder(MyDialogDemo.this)

            .setTitle("程序退出?")      //创建标题

            .setMessage("您确定要退出本程序吗?") //表示对话框中的内容

            .setIcon(R.drawable.pic_m) // 设置LOGO

            .setPositiveButton("确定", newDialogInterface.OnClickListener() {

                @Override

                public voidonClick(DialogInterface dialog, int which) {

                    MyDialogDemo.this.finish() ;    //操作结束

                }

            }).setNegativeButton("取消", newDialogInterface.OnClickListener() {

                @Override

                public voidonClick(DialogInterface dialog, int which) {

                   

                }

            }).create(); // 创建了一个对话框

        dialog.show() ; // 显示对话框

    }

}

单选按钮的对话框

publicclass MyDialogDemo extends Activity {

    private Button mybut = null ;   // 定义按钮

    private TextView mych = null ;  // 定义文本

    private String fruitData[] = new String[] {"苹果", "西瓜", "水蜜桃" };

    @Override

    public void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main); // 调用布局管理器

        this.mybut = (Button)super.findViewById(R.id.mybut) ;  //取得按钮

        this.mych = (TextView)super.findViewById(R.id.mych) ;  //取得文本

        this.mybut.setOnClickListener(newOnClickListenerImpl()) ;  //设置事件类

    }

    private class OnClickListenerImpl implementsOnClickListener {

 

        @Override

        public void onClick(View view) {

            Dialog dialog = newAlertDialog.Builder(MyDialogDemo.this)

                .setIcon(R.drawable.pic_m)

                .setTitle("请选择你喜欢吃的水果?")

                .setNegativeButton("取消", new DialogInterface.OnClickListener(){

                    @Override

                    public voidonClick(DialogInterface dialog, int which) {

                       

                    }

                }).setItems(MyDialogDemo.this.fruitData,new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterfacedialog, int which) {

                                    MyDialogDemo.this.mych

                                            .setText("您选择的水果是:"

                                                    +MyDialogDemo.this.fruitData[which]);

                    }

                }).create() ;

            dialog.show() ;

        }

       

    }

 

}


0 0
原创粉丝点击