Android studio button 按钮 四种绑定事件的方法
程序员文章站
2022-04-09 18:39:59
package com.geli_2.sujie.sujiegeili2testbutton; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; i... ......
package com.geli_2.sujie.sujiegeili2testbutton; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import android.widget.toast; public class mainactivity extends appcompatactivity implements view.onclicklistener { private button mbtclick; private button mbtclick2; private button mbtclick3; private button mbtclick4; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mbtclick = (button) findviewbyid(r.id.btntest1); //1、fdv 找到button id // 2、 alt ctrl f 形成类全局field mbtclick.setonclicklistener(new view.onclicklistener() //3、键入此行代码自动嵌入内部类方法 { @override public void onclick(view v) { //4、 toast tab tab 调出toast 方法 以上为方法一 toast.maketext(mainactivity.this, "hello world", toast.length_short).show(); } }); mbtclick2 = (button) findviewbyid(r.id.btnsample2); //5、找出button 2 的 button id fdv mbtclick2.setonclicklistener(new button_2_onclicklistener()); //9、实例化方法给button 绑定 以上是方法二 mbtclick3 = (button) findviewbyid(r.id.btntry3); //10、找出button 3 的 button id fdv mbtclick3.setonclicklistener(this); //11、 this 表示 main acitivity //12 但是这里是方法参数是onclicklistener接口,此处 this alt enter 会在此方法上加上实现的接口 mbtclick4 = (button) findviewbyid(r.id.btnexample4); //5、找出button 4 的 button id fdv } /** * called when a view has been clicked. * * @param v the view that was clicked. */ @override public void onclick(view v) { //方法三 } class button_2_onclicklistener implements view.onclicklistener //6、设置一个 click 方法 命名后继承自 view // onclicklistener //7、 后在 继承 implements 上 alt + enter 来实现方法 { /** * called when a view has been clicked. * * @param v the view that was clicked. */ @override public void onclick(view v) { //方法二 } } public void click(view view) { //方法四 } }
上一篇: CONTEST1001 题解