Android studio button 按钮 四种绑定事件的方法【实例代码】
Button是Android中一个非常简单的控件,在我们平时的项目中,可以说是非常的常见,使用率也是相当高。下面通过实例代码给大家介绍Androidstudiobutton按钮四种绑定事件的方法,具体代码如下所示:
packagecom.geli_2.sujie.sujiegeili2testbutton; importandroid.os.Bundle; importandroid.support.v7.app.AppCompatActivity; importandroid.view.View; importandroid.widget.Button; importandroid.widget.Toast; publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener { privateButtonmBtClick; privateButtonmBtClick2; privateButtonmBtClick3; privateButtonmBtClick4; @Override protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtClick=(Button)findViewById(R.id.btnTest1);//1、fdv找到buttonid //2、AltCtrlF形成类全局Field mBtClick.setOnClickListener(newView.OnClickListener()//3、键入此行代码自动嵌入内部类方法 { @Override publicvoidonClick(Viewv) {//4、Toasttabtab调出toast方法以上为方法一 Toast.makeText(MainActivity.this,"helloworld",Toast.LENGTH_SHORT).show(); } }); mBtClick2=(Button)findViewById(R.id.btnSample2);//5、找出button2的buttonidfdv mBtClick2.setOnClickListener(newButton_2_OnClickListener());//9、实例化方法给button绑定以上是方法二 mBtClick3=(Button)findViewById(R.id.btnTry3);//10、找出button3的buttonidfdv mBtClick3.setOnClickListener(this);//11、this表示mainacitivity //12但是这里是方法参数是onclicklistener接口,此处thisAltEnter会在此方法上加上实现的接口 mBtClick4=(Button)findViewById(R.id.btnExample4);//5、找出button4的buttonidfdv } /** *Calledwhenaviewhasbeenclicked. * *@paramvTheviewthatwasclicked. */ @Override publicvoidonClick(Viewv) { //方法三 } classButton_2_OnClickListenerimplementsView.OnClickListener//6、设置一个click方法命名后继承自view //onclicklistener //7、后在继承implements上alt+enter来实现方法 { /** *Calledwhenaviewhasbeenclicked. * *@paramvTheviewthatwasclicked. */ @Override publicvoidonClick(Viewv) { //方法二 } } publicvoidclick(Viewview) { //方法四 } }
总结
以上所述是小编给大家介绍的Androidstudiobutton按钮四种绑定事件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!