最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Button按鈕的四種點(diǎn)擊事件

 更新時(shí)間:2017年01月15日 11:45:53   作者:GaryHuang0306  
這篇文章主要為大家詳細(xì)介紹了Android Button按鈕的四種點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了安卓Button按鈕的四種點(diǎn)擊事件,供大家參考,具體內(nèi)容如下

第一種:內(nèi)部類實(shí)現(xiàn)

1.xml里面先設(shè)置Button屬性

<Button
 android:id="+@id/button1";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)

3.給Button設(shè)置一個(gè)點(diǎn)擊事件

btn.setOnClickListener(new MyClickListener()) //傳入的是ClickListener參數(shù)所以我們必須去定義一個(gè)參數(shù)接口

4.定義一個(gè)類去實(shí)現(xiàn) 按鈕需要的接口類型

public MianActivity extend Activity(){
...
...
private class MyClickListener()implent OnclickListener{
 //當(dāng)按鈕被點(diǎn)擊的時(shí)候調(diào)用
 public void Onclick (View v){
  //這里寫點(diǎn)擊事件方法
  System.out.printLn("被點(diǎn)擊了")
  }
}
 }

第二種:利用匿名內(nèi)部類來(lái)實(shí)現(xiàn)

1.xml里面先設(shè)置Button屬性

<Button
 android:id="+@id/button1";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1);

3.給Button設(shè)置一個(gè)點(diǎn)擊事件

//匿名內(nèi)部類
public MianActivity extend Activity(){
...
...
btn.setOnClickListener(new OnClickListener(){
 public void Onclick (View v){
  //這里寫點(diǎn)擊事件方法
  System.out.printLn("被點(diǎn)擊了")

  }
} )
  };

第三種:Activity實(shí)現(xiàn)OnclickListener接口適用于多個(gè)按鈕情況

1.xml里面先設(shè)置Button屬性

<Button
 android:id="+@id/button1";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕"/>
<Button
 android:id="+@id/button2";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕2"/>
 <Button
 android:id="+@id/button1";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕3"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)
Button btn2 =(Button)findViewById(R.layout.button2)
Button btn3 =(Button)findViewById(R.layout.button3)

3.給Button設(shè)置一個(gè)點(diǎn)擊事件

public MianActivity extend Activity implement OnClickListener(){
  ...
  ...
  Button btn =(Button)findViewById(this);//this代表MainActivity
  Button btn2 =(Button)findViewById(this)
  Button btn3 =(Button)findViewById(this)

  public void Onclick (View v){
  //具體判斷點(diǎn)擊的是哪個(gè)按鈕
  switch(v.getId()){
  case.R.id.button1://代表點(diǎn)擊第一個(gè)按鈕
   TODO();//實(shí)現(xiàn)具體方法
   break;
  case.R.id.button2:
   TODO();//實(shí)現(xiàn)具體方法
   break;
  case.R.id.button3:
   TODO();//實(shí)現(xiàn)具體方法
   break;  
  default:
   break;
  }

  }
  private void TODO(){
   //具體方法
  }
}

第四種:在xml里面聲明onclick

1.xml里面先設(shè)置Button屬性

<Button
 android:id="+@id/*button1*";
 android:layout_width="wrap_parent";
 android:layout_height="wrap_parent"
 android:text="按鈕"
 android:onClick="click"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)

3.聲明一個(gè)方法,方法名和你要點(diǎn)擊的這個(gè)按鈕在xml布局中聲明的Onclick屬性一樣

public void **click**(View v){
 TODO();//實(shí)現(xiàn)具體方法
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android調(diào)用前后攝像頭同時(shí)工作實(shí)例代碼

    Android調(diào)用前后攝像頭同時(shí)工作實(shí)例代碼

    本篇文章主要介紹了Android調(diào)用前后攝像頭同時(shí)工作實(shí)例代碼,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-07-07
  • Android自定義View葉子旋轉(zhuǎn)完整版(六)

    Android自定義View葉子旋轉(zhuǎn)完整版(六)

    這篇文章主要為大家詳細(xì)介紹了Android自定義View葉子旋轉(zhuǎn)完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android實(shí)現(xiàn)平滑翻動(dòng)效果

    Android實(shí)現(xiàn)平滑翻動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)平滑翻動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android 中的 XRecyclerview的使用案例

    Android 中的 XRecyclerview的使用案例

    這篇文章主要介紹了Android 中的 XRecyclerview的使用案例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • android實(shí)現(xiàn)狀態(tài)欄添加圖標(biāo)的函數(shù)實(shí)例

    android實(shí)現(xiàn)狀態(tài)欄添加圖標(biāo)的函數(shù)實(shí)例

    這篇文章主要介紹了android實(shí)現(xiàn)狀態(tài)欄添加圖標(biāo)的函數(shù),較為詳細(xì)的分析了Android狀態(tài)欄添加及刪除圖標(biāo)的具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android高效安全加載圖片的方法詳解

    Android高效安全加載圖片的方法詳解

    Android開發(fā)中消耗內(nèi)存較多一般都是在圖像上面,下面這篇文章主要給大家介紹了關(guān)于Android如何高效安全加載圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Kotlin如何直接使用控件ID原理詳析

    Kotlin如何直接使用控件ID原理詳析

    這篇文章主要給大家介紹了關(guān)于Kotlin如何直接使用控件ID原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Android編程之四種Activity加載模式分析

    Android編程之四種Activity加載模式分析

    這篇文章主要介紹了Android編程之四種Activity加載模式,簡(jiǎn)要分析了Android編程中涉及的Activity的四種加載模式,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-01-01
  • Android如何設(shè)置圓角圖片

    Android如何設(shè)置圓角圖片

    這篇文章主要為大家詳細(xì)介紹了Android如何設(shè)置圓角圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能

    Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能

    這篇文章主要為大家詳細(xì)介紹了Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論

吐鲁番市| 镇平县| 肥乡县| 眉山市| 长宁县| 隆林| 禄劝| 迁安市| 河西区| 高邑县| 新营市| 大石桥市| 淳安县| 大安市| 新巴尔虎左旗| 崇礼县| 华亭县| 醴陵市| 依安县| 石林| 昭平县| 德清县| 泽库县| 晋江市| 丹东市| 嘉祥县| 五常市| 乐业县| 渭源县| 池州市| 邵阳市| 大安市| 望城县| 临邑县| 扶风县| 连城县| 万全县| 乡宁县| 乐清市| 罗田县| 洪雅县|