国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

三種方式實現控件監聽

2019-11-06 09:37:47
字體:
來源:轉載
供稿:網友

轉載請注明出處:http://blog.csdn.net/mr_leixiansheng/article/details/58597469

1、匿名內部類監聽

2、外部類監聽

3、接口實現監聽

代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button"        android:text="常規按鈕"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <ImageButton        android:id="@+id/image_button"        android:src="@mipmap/ic_launcher"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>
package com.example.leixiansheng.buttonimagebutton;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.Toast;/** * Created by Leixiansheng on 2017/2/28. *//** * 1、明白Button和ImageButton的用法和區別 * 2、3種監聽方式(內部監聽、外部監聽、接口監聽) */public class MainActivity extends AppCompatActivity {    PRivate Button button;    private ImageButton imageButton;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_activity);        button = (Button) findViewById(R.id.button);        imageButton = (ImageButton) findViewById(R.id.image_button);        //1、內部類監聽(直接new View.OnClickListener使用)//        button.setOnClickListener(new View.OnClickListener() {//            @Override//            public void onClick(View view) {//                Toast.makeText(MainActivity.this, "點擊了按鈕",Toast.LENGTH_SHORT).show();//            }//        });//        imageButton.setOnClickListener(new View.OnClickListener() {//            @Override//            public void onClick(View view) {//                Toast.makeText(MainActivity.this, "點擊了按鈕",Toast.LENGTH_SHORT).show();//            }//        });        //2、外部類監聽(1、外面先寫一個類并繼承監聽 2、調用這個外部類實現監聽)        // 作用:解決代碼復用,比如每個按鍵都需要實現點擊后變透明,只需一個外部類即可        MyListener listener = new MyListener();        button.setOnClickListener(listener);        imageButton.setOnClickListener(new MyListener(){            @Override            public void onClick(View view) {                super.onClick(view);                Toast.makeText(view.getContext(), "點擊了特殊按鈕",Toast.LENGTH_SHORT).show();            }        });        //3、接口監聽(實現View.OnClickListener)//        button.setOnClickListener(this);//        imageButton.setOnClickListener(this);    }//      接口實現監聽//    @Override//    public void onClick(View view) {//        switch (view.getId()) {//            case R.id.button:////                break;//              case R.id.image_button:////                break;////        }//    }}//外部類實現監聽class MyListener implements View.OnClickListener {    @Override    public void onClick(View view) {        view.setAlpha(0.5f);    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五指山市| 保亭| 平湖市| 招远市| 红安县| 吴旗县| 红河县| 龙山县| 宜君县| 陇川县| 辽宁省| 和林格尔县| 南昌市| 井研县| 油尖旺区| 彭州市| 霍邱县| 三亚市| 清新县| 格尔木市| 香格里拉县| 平昌县| 铁力市| 康定县| 阿拉善盟| 昌宁县| 余干县| 永宁县| 石楼县| 衡山县| 琼中| 萨迦县| 肃北| 西城区| 齐齐哈尔市| 上饶县| 泽库县| 封丘县| 延吉市| 兰西县| 寿宁县|