Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
Android中Button控件應(yīng)該算作是比較簡(jiǎn)單的控件,然而,它的使用頻率卻是非常的高,今天,我在這里總結(jié)了三種常用的點(diǎn)擊Button實(shí)現(xiàn)其功能的方法。
1.很多時(shí)候,我們?cè)谟玫紹utton控件時(shí),往往都是“一次性”使用,這時(shí),為了方便起見(jiàn),我們一般采用的是匿名內(nèi)部類的方法,形如這樣:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("您點(diǎn)擊了Button1");
}
});
我們可以看到,這樣的代碼不僅簡(jiǎn)短,而且清晰易懂,不過(guò),這樣的方法一般只是適用于這個(gè)Button使用的次數(shù)不多或是“一次性”使用
2.當(dāng)Button有多個(gè)或者Button的使用次數(shù)很多時(shí),我們需要采用綁定監(jiān)聽(tīng)器的做法,其實(shí),綁定監(jiān)聽(tīng)器也有幾種方法,不過(guò),我在這里就不一一列舉了,畢竟那些方法在實(shí)際的應(yīng)用中也不常見(jiàn)。
我們一般的方法是實(shí)現(xiàn)OnClickListener接口,并實(shí)現(xiàn)其中的方法,正如這樣:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button2:
System.out.println("您點(diǎn)擊了Button2");
break;
default:
break;
}
}
注:onClick方法是OnClickListen接口中的方法,我們實(shí)現(xiàn)這個(gè)接口就必須實(shí)現(xiàn)它的方法。
3.這是一種最為簡(jiǎn)單的方法,我們需要做的就是添加一個(gè)方法并為Button添加一個(gè)屬性:
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3 測(cè)試"
android:onClick="clickHandler"
/>
其中,我們比平時(shí)多添加了onClick屬性。
那么,我們需要在代碼中添加我們?cè)趯傩灾新暶鞯姆椒ǎ?BR>
public void clickHandler(View view) {
System.out.println("您點(diǎn)擊了Button3");
}
最后,貼出完整的源代碼和實(shí)現(xiàn)效果截圖:
1.布局文件
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1 測(cè)試"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2 測(cè)試"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3 測(cè)試"
android:onClick="clickHandler"
/>
</LinearLayout>
效果形如:

2.測(cè)試源代碼
package com.example.buttonclicktest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button button1 = null;
private Button button2 = null;
public void findButton() {
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findButton();
button2.setOnClickListener(this);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("您點(diǎn)擊了Button1");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button2:
System.out.println("您點(diǎn)擊了Button2");
break;
default:
break;
}
}
public void clickHandler(View view) {
System.out.println("您點(diǎn)擊了Button3");
}
}
當(dāng)我們點(diǎn)擊按鈕后,在Logcat中我們可以查看到結(jié)果如下所示:

從結(jié)果中我們可以看出,三種方法都可以實(shí)現(xiàn)按鈕點(diǎn)擊的功能,我們可以根據(jù)情況的不同選擇相應(yīng)的方法。
- Android控件ToggleButton多狀態(tài)按鈕使用詳解
- Android ToggleButton 詳解及實(shí)例代碼
- android基本控件ToggleButton&Switch使用指南
- Android控件之ToggleButton的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android RadioButton單選框的使用方法
- android RadioButton和CheckBox組件的使用方法
- Android開(kāi)發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android ImageButton自定義按鈕的按下效果的代碼實(shí)現(xiàn)方法分享
- android自定義按鈕示例(重寫(xiě)imagebutton控件實(shí)現(xiàn)圖片按鈕)
- Android開(kāi)發(fā)之ToggleButton實(shí)現(xiàn)開(kāi)關(guān)效果示例
相關(guān)文章
如何使用Matrix對(duì)bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)
本篇文章是對(duì)使用Matrix對(duì)bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android在OnCreate中獲取控件的寬度和高度的實(shí)現(xiàn)代碼
在Android中,有時(shí)需要對(duì)控件進(jìn)行測(cè)量,得到的控件寬度和高度可以用來(lái)做一些計(jì)算。在需要自適應(yīng)屏幕的情況下,這種計(jì)算就顯得特別重要2012-11-11Flutter中如何加載并預(yù)覽本地的html文件的方法
這篇文章主要介紹了Flutter中如何加載并預(yù)覽本地的html文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11基于Android10渲染Surface的創(chuàng)建過(guò)程
這篇文章主要介紹了基于Android10渲染Surface的創(chuàng)建過(guò)程,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Android通過(guò)ConnectivityManager檢查網(wǎng)絡(luò)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)ConnectivityManager檢查網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08Android鍵盤(pán)自動(dòng)彈出解決方法分析
這篇文章主要介紹了Android鍵盤(pán)自動(dòng)彈出解決方法,結(jié)合實(shí)例形式對(duì)比分析了三種解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01