android学习笔记6.06

new View.OnClickListener()

在Android程序中会经常用到这个语句,为什么前面要加个View呢,原因就是后面的OnClickListener是个View类内部的接口,如果直接使用是找不到这个接口的。
使用了匿名内部类
下面代码中顺序不可以错,不然会抛出异常

requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

logcat

使用真机调试打印无法打印logcat日志操作:
在拨号界面输入*#*#2846579#*#* 进入工程菜单,然后点击log设置,全部勾选就可以使用!!

用Bundle传递数据

 Bundle bundle = new Bundle();
                    String tempData = "Something you typed";
                    bundle.putString("data_key",tempData);
                    intent.putExtras(bundle);

        startActivity(intent);

目标活动中、

 Intent intent = getIntent();
        Bundle outState = intent.getExtras();
        String tempData = outState.getString("data_key");
        Log.d("NormalActivity",tempData);

新建一个AlertDialog

代码如下:

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View V) {
             //创建dialog的实例
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("This is a dialog");
                dialog.setMessage("Something important");
                dialog.setCancelable(false);
                //创建positivebutton,参数以为显示的字符,参数而为一个Listner的对象,由于OnClickListner是DialogInterface的内部类,所以用这种方法调用创建对象
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,int which) {

                    }
                });

                dialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog,int which) {

                    }
                });

                dialog.show();
            }
        });

setCancelable()用法

setCancelable
public AlertDialog.Builder setCancelable (boolean cancelable)
Since: API Level 1
Sets whether the dialog is cancelable or not default is true.
设置为false,按返回键不能退出。默认为true。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值