android按鈕響應事件嗎,Android 按鈕響應事件的幾種方式

目錄

1.在布局中指定onClick屬性

布局代碼

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button1"

android:onClick="click"

/>

android:id="@+id/btn2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button2"

android:layout_below="@+id/btn1"

android:onClick="click"

/>

邏輯代碼

public void click(View v){

if(v.getId()==R.id.btn1)

Toast.makeText(this,"button1 is clicked",Toast.LENGTH_SHORT).show();

if(v.getId()==R.id.btn2)

Toast.makeText(this,"button2 is clicked",Toast.LENGTH_SHORT).show();

}

2.使用匿名內部類

public class MainActivity extends AppCompatActivity {

private Button btn1;

private Button btn2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.buttonclick);

//通過findViewById()初始化控件

btn1=(Button) findViewById(R.id.btn1);

btn2=(Button) findViewById(R.id.btn2);

//匿名內部類

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

btn1.setText("button1 is clicked");

}

});

btn2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

btn2.setText("button2 is clicked");

}

});

}

}

3.在當前Activity中實現OnClickListener接口

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button btn1;

private Button btn2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.buttonclick);

//通過findViewById()初始化控件

btn1=(Button) findViewById(R.id.btn1);

btn2=(Button) findViewById(R.id.btn2);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

}

public void onClick(View v){

if(v.getId()==R.id.btn1){

Toast.makeText(this,"button1 is clicked",Toast.LENGTH_SHORT).show();

}

if(v.getId()==R.id.btn2){

Toast.makeText(this,"button2 is clicked",Toast.LENGTH_SHORT).show();

}

}

}

4.創建內部類

public class MainActivity extends AppCompatActivity {

private Button btn1;

private Button btn2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.buttonclick);

//通過findViewById()初始化控件

btn1=(Button) findViewById(R.id.btn1);

btn2=(Button) findViewById(R.id.btn2);

//傳入實現了OnClickListener接口的類的對象

btn1.setOnClickListener(new MyClickListener());

btn2.setOnClickListener(new MyClickListener());

}

//創建內部類實現OnClickListener接口

private class MyClickListener implements View.OnClickListener{

@Override

public void onClick(View v){

if(v.getId()==R.id.btn1)

btn1.setText("button1 is clicked");

if(v.getId()==R.id.btn2)

btn2.setText("button2 is clicked");

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457009.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457009.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457009.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android 開源優秀 Library 推薦

之后的每月會推薦一些優秀的開源 Android libraries,不僅僅是學習,也可以方便的使用到項目中,避免重復的造輪子。 PageIndicatorView 如果你需要在 ViewPager 中指明當前處于哪個畫面中,PageIndicatorView是你的不二選擇。而且真…

Vue2.0王者榮耀助手

vue-gok vue2.0-王者榮耀助手 項目使用的是帶玩游戲平臺提供的API,由于騰訊公司王者榮耀游戲關閉了查看其它人的戰績功能,帶玩平臺受其影響,已將API暫時關閉,所以無法獲得穩定的 DAIWAN-API-TOKEN ,所以會出現DAIWAN-API-TOKEN令…

訪問對象的屬性和方法

class Person(object):name ""age 0height 0weight 0def run(self):print("run")def eat(self,food):print("eat"food)def openDoor(self):print("我已經打開了門")def fillEle(self):print("我已經把大象裝進了冰箱")de…

Android mc怎么和win10聯機,我的世界手機版/win10版聯機完美互通方法

我的世界手機版玩家想要更加方便的操作?0.12.1更新之后,你除了能使用手柄進行方便的操作之外,現在你能完全擺脫小屏幕蹩腳的操作了!win10版在9月9日進行了重大更新,我們能直接使用win10的電腦玩我的世界,而…

十大基礎算法

做為程序員,以下著十大10大基礎實用算法是必須知道的。

[HNOI2004]L語言

1212: [HNOI2004]L語言 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1507 Solved: 666[Submit][Status][Discuss]Description 標點符號的出現晚于文字的出現,所以以前的語言都是沒有標點的。現在你要處理的就是一段沒有標點的文章。 一段文章T是由若干小寫字…

對象的初始狀態(構造函數)

class Person(object):# name ""# age 0# height 0# weight 0def run(self):print("run")def eat(self,food):print("eat"food)def __init__(self,name,age,height,weight):# print(name,age,weight,height)print("這里是init")sel…

【bzoj 2434】【codevs 1946】[Noi2011]阿貍的打字機(AC自動機)

2434: [Noi2011]阿貍的打字機 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2477 Solved: 1382[Submit][Status][Discuss]Description 阿貍喜歡收藏各種稀奇古怪的東西,最近他淘到一臺老式的打字機。打字機上只有28個按鍵,分別印有26個小寫英文字母…

android加法服務類,iOS越來越像Android:蘋果簡單做加法遠離精致

原標題:iOS越來越像Android:蘋果簡單做加法遠離精致剛剛結束的WWDC2016的主題演講中,蘋果為我們帶來了最新的iOS 10系統,官方稱本次iOS 10的推出有著多大10項的重要更新,在用戶體驗、界面、Siri、地圖以及音樂方面都有著不少的變化…

JDK源碼學習之Arraylist與LinkedList

ArrayList和LinkedList是我們在開發過程中常用的兩種集合類,本文將從底層源碼實現對其進行簡單介紹。 下圖是Java集合類所涉及的類圖。 一.ArrayList 從上面的集合類圖可以看出,ArrayList實現了List接口。ArrayList是順序的集合容器,容器中可以存放null…

學習記錄4

學習了python基本數據類型,附學習筆記圖及操作圖 轉載于:https://www.cnblogs.com/bgd140206127/p/6549229.html

self 實例對象-代碼詳細解釋

self代表類的實例,而非類哪個對象調用方法,那么該方法中的self就代表那個對象self.__calss__ 代表類名class Person(object):def run(self):print("run")print(self.__class__)p self.__class__("tt",30,10,20)print(p)def eat(sel…

CString之GetBuffer與ReleaseBuffer

我們知道,CString是MFC中提供的方便字符串操作的一個類,非常好使,具有自動動態內存管理功能。 GetBuffer()主要作用是將字符串的緩沖區長度鎖定; ReleaseBuffer()則是解除對緩沖區的鎖定,這樣使得CString對象在以后的代…

mac 編譯android系統,mac 編譯 Android 系統雜記

掛載android分區sudo hdiutil attach ~/android_code/android7.dmg.sparseimage -mountpoint /Volumes/android原放入U盤:echo 188jinghao | sudo -S hdiutil attach ~/android7.dmg.sparseimage -mountpoint /Volumes/android放入機械硬盤sudo hdiutil attach /Vol…

Java開發必須熟悉的Linux命令總結

身為一個Java開發人員,這些常用的Linux命令必須掌握。即使平時開發過程中沒有使用Linux(Unix)或者mac系統,也需要熟練掌握Linux命令。因為很多服務器上都是Linux系統。所以,要和服務器機器交互,就要通過she…

構析函數

析構函數:__del__() 釋放對象時自動調用 class Person(object):def run(self):print("run")def eat(self,food):print("eat"food)def __init__(self,name,age,height,weight):self.name nameself.height heightself.age ageself.weight …

Java 序列化Serializable詳解(附詳細例子)

Java 序列化Serializable詳解(附詳細例子) 1、什么是序列化和反序列化Serialization(序列化)是一種將對象以一連串的字節描述的過程;反序列化deserialization是一種將這些字節重建成一個對象的過程。 2、什么情況下需要…

kettle-實現每個分組的前N的數據

2019獨角獸企業重金招聘Python工程師標準>>> 第一步:創建表及數據: create table uid(uid int, --uidcate varchar(20), --類別price double --金額 ) insert into uid values(123,c1,21); insert into uid values(123,c2,23); insert into u…

重寫__repr__與__str__函數

重寫:將函數重新定義寫一遍__str__():再調用print 打印對象時自動調用,是給用戶用的是一個描述對象的方法__repr__():是給機器用的,在python解釋器里面直接敲對象名再回車調用的方法注意:在沒有str時,且有repr,str re…

linux nexus 使用問題

2019獨角獸企業重金招聘Python工程師標準>>> 問題一,啟動提示設置RUN_AS_USERroot 但是,設置export或 /etc/profile未生效。 **************************************** WARNING - NOT RECOMMENDED TO RUN AS ROOT *************************…