目录
public class mainactivity extends appcompatactivity { private button bt; private textview tv; int count=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bt = (button)findviewbyid(r.id.bt1); tv = (textview)findviewbyid(r.id.hello); //生成监听器对象 new buttonlistener() //为控件绑定监听器对象 bt.setonclicklistener bt.setonclicklistener(new buttonlistener()); system.out.println("--mainactivity: oncreate--"); } // 定义一个类,实现监听器接口 class buttonlistener implements view.onclicklistener{ @override public void onclick(view v) { count++; tv.settext(count+""); } } }
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000" android:text="first text view"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text=" second text view"/> </linearlayout>
px: 像素分辨率,屏幕是480*800个像素,每个像素可以显示一个rgb颜色
dpi:屏幕细腻程度
dp:设备无关像素(最主要)
为什么使用dp?
private checkbox eatbox, sleppbox, dotabox; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.frist_layout); eatbox = (checkbox)findviewbyid(r.id.eatid); sleppbox = (checkbox)findviewbyid(r.id.sleppid); dotabox = (checkbox)findviewbyid(r.id.dotaid); onboxclicklistener listener = new onboxclicklistener(); eatbox.setonclicklistener(listener); sleppbox.setonclicklistener(listener); dotabox.setonclicklistener(listener); }
配置文件如下
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <checkbox android:id="@+id/eatid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃饭"/> <checkbox android:id="@+id/sleppid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="睡觉"/> <checkbox android:id="@+id/dotaid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="data"/> </linearlayout>
onclicklistener
和oncheckedchangelistener
监听器onclicklistener
监听器class onboxclicklistener implements view.onclicklistener{ // view参数是调用setonclicklistener的对象 // view是checkbox的父类 // view.getid--查看是哪个对象调用的这个方法 @override public void onclick(view v) { // 向下转型 checkbox box = (checkbox)v; if (v.getid() == r.id.eatid){ system.out.println("eat is clicked"); } else if (v.getid() == r.id.sleppid){ system.out.println("slepp is clicked"); } else if (v.getid() ==r.id.dotaid){ system.out.println("dota is clicked"); } // checkbox 是否选中 if (box.ischecked()){ system.out.println("clicked"); } else{ system.out.println("not clicked"); } } }
oncheckedchangelistener
监听器compoundbutton
// 选中的时候就会调用这个状态 class checkboxlistener implements compoundbutton.oncheckedchangelistener{ @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (buttonview.getid() == r.id.eatid){ system.out.println("eat is clicked"); } else if (buttonview.getid() == r.id.sleppid){ system.out.println("slepp is clicked"); } else if (buttonview.getid() ==r.id.dotaid){ system.out.println("dota is clicked"); } // checkbox 是否选中 if (ischecked){ system.out.println("clicked"); } else{ system.out.println("not clicked"); } } }
如对本文有疑问, 点击进行留言回复!!
荐 面试官:kafka日志段如何读写都不知道,好意思说精通Kafka吗?
荐 如何写出一份“有理有据使人信服”的Android项目设计文档
荐 灵魂一问,Android中有代替HashMap的方法吗?
intent.setFlag的参数 FLAG_ACTIVITY_CLEAR_TASK 说明
网友评论