Android開發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽方法總結(jié)
本文實(shí)例總結(jié)了Android開發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽方法。分享給大家供大家參考,具體如下:
先來介紹Button事件實(shí)現(xiàn)的兩種方法
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/myButton1" android:text=" 按鈕1 " android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/myButton2" android:text=" 按鈕2 " android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ButtonDemoActivity!</string> <string name="app_name">ButtonDemo</string> </resources>
第一種:
ButtonDemoActivity.java:
package com.android.ButtonDemo.activity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ButtonDemoActivity extends Activity { Button myButton1,myButton2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1=(Button)findViewById(R.id.myButton1); myButton2=(Button)findViewById(R.id.myButton2); //使用匿名類注冊(cè)Button事件 myButton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(ButtonDemoActivity.this, "你點(diǎn)擊了按鈕1",Toast.LENGTH_LONG).show(); } }); myButton2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(ButtonDemoActivity.this, "你點(diǎn)擊了按鈕2",Toast.LENGTH_LONG).show(); } }); } }
第二種:
ButtonDemoActivity.java:
package com.android.ButtonDemo.activity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ButtonDemoActivity extends Activity { Button myButton1,myButton2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1=(Button)findViewById(R.id.myButton1); myButton2=(Button)findViewById(R.id.myButton2); myButton1.setOnClickListener(new ButtonClick()); myButton2.setOnClickListener(new ButtonClick()); } //創(chuàng)建一個(gè)類,來響應(yīng)OnClickListener class ButtonClick implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case R.id.myButton1: Toast.makeText(ButtonDemoActivity.this, "你點(diǎn)擊了按鈕1",Toast.LENGTH_LONG).show(); break; case R.id.myButton2: Toast.makeText(ButtonDemoActivity.this, "你點(diǎn)擊了按鈕2",Toast.LENGTH_LONG).show(); break; default: break; } } } }
再來說說Button監(jiān)聽方法
android button控件目前主要有如下幾種監(jiān)聽方式:
一個(gè)button控件對(duì)應(yīng)一個(gè)監(jiān)聽:
Button buttontest; buttontest = (Button) findViewById(R.id.button1); buttontest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("TEST", "button onClick"); } });
多個(gè)button對(duì)應(yīng)一個(gè)監(jiān)聽1:
start = (Button) findViewById(R.id.button1); stop = (Button) findViewById(R.id.button2); start.setOnClickListener(mylistener ); stop.setOnClickListener(mylistener ); View.OnClickListener mylistener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Log.d(TAG, "Start to recorder video\n"); start_recorde(); break; case R.id.button2: Log.d(TAG, "Stop to recorder video\n"); stop_recorde(); break; default: break; } } };
多個(gè)button對(duì)應(yīng)一個(gè)監(jiān)聽2:
public class MainActivity extends Activity implements View.OnClickListener{ //界面元素 private Button start; private Button stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.button1); stop = (Button) findViewById(R.id.button2); start.setOnClickListener(this); stop.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Log.d(TAG, "Start to recorder video\n"); break; case R.id.button2: Log.d(TAG, "Stop to recorder video\n"); break; default: break; } } }
xml中綁定監(jiān)聽:
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="mybuttonlistener"> </Button>
對(duì)應(yīng)java代碼如下:
Button btn = (Button) findViewById(R.id.button1); public void mybuttonlistener(View target){ //do something5 }
從個(gè)人角度來講,不推薦使用第四種實(shí)現(xiàn)方式。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義Button并設(shè)置不同背景圖片的方法
- Android實(shí)現(xiàn)圓角Button按鈕
- Android實(shí)現(xiàn)button居中的方法
- Android實(shí)現(xiàn)自定義帶文字和圖片Button的方法
- Android開發(fā)之button事件監(jiān)聽簡(jiǎn)單實(shí)例
- 解決Eclipse創(chuàng)建android項(xiàng)目無法正常預(yù)覽布局文件問題的方法
- Android編程之代碼創(chuàng)建布局實(shí)例分析
- Android實(shí)時(shí)文件夾創(chuàng)建方法
- Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
- Android創(chuàng)建Alert框的方法
- Android開發(fā)之創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法
相關(guān)文章
Kotlin面向?qū)ο笾R(shí)點(diǎn)講解
面向?qū)ο缶幊掏ㄟ^對(duì)事物的抽象,大大的簡(jiǎn)化了程序的開發(fā)難度。我們常用的編程語言:Java、C++、Python都屬于面向?qū)ο缶幊?。Kotlin與java類似,也是一種面向?qū)ο缶幊陶Z言。本文從面向?qū)ο笕齻€(gè)基本特征:封裝、繼承、多態(tài),來闡述一下Kotlin中的面向?qū)ο缶幊?/div> 2022-12-12Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左側(cè)滑動(dòng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android中關(guān)于Notification及NotificationManger的詳解
本篇文章小編為大家介紹,Android中關(guān)于Notification及NotificationManger的詳解。需要的朋友參考下2013-04-04Android使用google breakpad捕獲分析native cash
這篇文章主要介紹了Android使用google breakpad捕獲分析native cash 的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Android實(shí)現(xiàn)中國(guó)象棋附源碼下載
這篇文章主要詳細(xì)介紹了Android實(shí)現(xiàn)中國(guó)象棋的具體代碼,供大家參考,感興趣的小伙伴們可以參考一下2016-05-05Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成
這篇文章主要介紹了Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android獲取點(diǎn)擊屏幕的位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05解決Android自定義view獲取attr中自定義顏色的問題
這篇文章主要介紹了Android自定義view獲取attr中自定義顏色的問題解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12最新評(píng)論