zoukankan      html  css  js  c++  java
  • 监听事件的三种实现方式

    1、在activity_main.xml文件中添加三个Button按钮

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn"
            />
    
    </LinearLayout>

    2、

    第一种方式:通过内部类实现事件监听
            /*
            第一种方式:通过内部类实现事件监听
             */
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("tag","第一种方式:通过内部类实现事件监听");
                    Toast.makeText(MainActivity.this,"第一个按钮被点击",Toast.LENGTH_SHORT).show();
                }
            });

    3、

    第二种方式:通过外部类实现事件监听(独立类实现)
            /*
            第二种方式:通过外部类实现事件监听(独立类实现)
             */
            btn2.setOnClickListener(new MyOnClickListener(){
                @Override
                public void onClick(View v) {
                    super.onClick(v);
                    Log.i("tag", "第二种方式:通过内部类实现事件监听");
                }
            });
    
        class MyOnClickListener implements View.OnClickListener{
    
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"第二个按钮被点击",Toast.LENGTH_SHORT).show();
            }
        }

    说明:第二种方式:通过外部类实现事件监听(独立类实现),一般有多个组件要实现同一个方法时,才采用这种方法来实现事件监听。

    4、

    第三种方式:通过OnClickListener接口实现事件监听
    public class MainActivity extends Activity implements View.OnClickListener{
       private Button btn3;
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
       btn3.setOnClickListener(this);
    }
    
     /*
        第三种方式:通过OnClickListener接口实现事件监听
         */
        @Override
        public void onClick(View v) {
            Log.i("tag", "第三种方式:通过OnClickListener接口实现事件监听");
        }
    }

    说明: btn3.setOnClickListener(this);其中this就是实现OnClickListener下的onClick方法。

  • 相关阅读:
    hdoj-1004-Let the Balloon Rise(水题)
    hdoj-1827-Summer Holiday(scc+缩点)
    poj--3624--Charm Bracelet(动态规划 水题)
    HDU
    HDU
    HDU
    HDU
    【POJ1654】Area【叉积】
    【POJ1654】Area【叉积】
    【SSLOJ1715】计算面积【叉积】
  • 原文地址:https://www.cnblogs.com/tonycheng93/p/4558775.html
Copyright © 2011-2022 走看看