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。