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) { //方法四 } }
原文地址:https://www.cnblogs.com/2eggs/p/9517601.html
时间: 2024-10-08 17:35:42