Android 監聽按鈕的點擊事件

來源:互聯網
上載者:User

標籤:

onClick事件
1.Button和ImageButton都擁有一個onClick事件
 通過自身的.setOnClickListener(OnClickListener)方法添加點擊事件
2.所有的控制項都有一個onclick事件
 不僅僅Button和ImageButton擁有
3.通過點擊事件的監聽可以實現點擊按鈕之後要發生什麼動作

監聽事件實現的集中寫法
1.匿名內部類的實現
2.獨立類的實現
3.實現介面的方式來實現

package com.example.buttonimagebutton;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v7.app.ActionBarActivity;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageButton;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    private Button button1;    private ImageButton imageButton1;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                // button1 實現 匿名內部類        button1 = (Button) findViewById(R.id.button1);        button1.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                int red = (int) (Math.random() * 255.9);                int green = (int) (Math.random() * 255.9);                int blue = (int) (Math.random() * 255.9);                button1.setBackgroundColor(Color.rgb(red, green, blue));            }                    });                // button2 實現 外部類 MyOnClickListener        imageButton1 = (ImageButton) findViewById(R.id.imageButton1);        imageButton1.setOnClickListener(new MyOnClickListener(this));            }}class MyOnClickListener implements OnClickListener {        private MainActivity context;        public MyOnClickListener(MainActivity context) {        this.context = context;    }    @Override    public void onClick(View arg0) {        Toast.makeText(context, "Image按鈕(ImageButton)要執行的邏輯", 1).show();    }}
MainActivity.java

其中,button1用於實現匿名內部類,它的作用是每當點擊一下button1它的背景顏色就是隨機改變;

imageButton1用於實現獨立(自訂)的監聽事件的類(MyOnClickListener implements OnClickListener),它用於在MainActivity中顯示一個提示資訊(Toast)。

<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:orientation="vertical"    >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="按鈕1" />        <ImageButton        android:id="@+id/imageButton1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" /></LinearLayout>
activity_main.xml

效果:

Android 監聽按鈕的點擊事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.