久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Android 一個(gè)用于多個(gè)按鈕的 OnClick 方法?

Android one OnClick method for multiple buttons?(Android 一個(gè)用于多個(gè)按鈕的 OnClick 方法?)
本文介紹了Android 一個(gè)用于多個(gè)按鈕的 OnClick 方法?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在android中開始了一點(diǎn)程序,我在一個(gè)活動(dòng)中有 3 個(gè)按鈕.

I started program little bit in android, I have 3 buttons in a single activity.

我看到一些示例代碼將相同的 OnClick 事件分配給所有按鈕(即使它們執(zhí)行完全不同的操作)并且在方法 Switch(id) 的情況下案例案例...

I saw some example codes that assign the same OnClick event to all the buttons (even if they perform completely different action) and in the method Switch(id) case case case...

什么是更好的方法?一個(gè)onClick方法和切換還是很多方法,每個(gè)按鈕一個(gè)?

What is the better approach? one onClick method and switching or a lot of methods, one for each button?

謝謝.

推薦答案

如果你想減少代碼行數(shù)那么使用 View 的 OnClick() 和 switch 語句 如果你想單獨(dú)處理所有點(diǎn)擊(便于理解和維護(hù)代碼)然后使用單獨(dú)的所有按鈕的onClick().

If you want to reduce the coding lines then use View's OnClick() with switch statement and if you want to handle separately all click (for easily understanding and maintaining code) then use separate all button's onClick().

更新:

如果您在 Activity 布局 xml 文件中聲明了按鈕,則為所有按鈕編寫具有相同方法名稱的屬性 android:onClick="" 并在您的 Activity 中實(shí)現(xiàn)該方法.現(xiàn)在你有一個(gè)所有按鈕的方法,并在該方法中區(qū)分具有 id 的按鈕.

If you have declared Buttons in your Activity layout xml file, than write attribute android:onClick="" with same method name for all buttons and implement that method in your activity. Now you have one method for all buttons and in that method differentiate buttons with id.

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 1" />
    <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 2" />
    <Button android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 3" />
</LinearLayout>

現(xiàn)在在您的 Activity 中實(shí)現(xiàn) buttonOnClick 之類的,

Now in your Activity implement buttonOnClick like,

public void buttonOnClick(View view)
{
 switch(view.getId())
 {
  case R.id.button1:
  // Code for button 1 click
  break;

  case R.id.button2:
  // Code for button 2 click
  break;

  case R.id.button3:
  // Code for button 3 click
  break;
 }
}

或者您可以為活動(dòng)中動(dòng)態(tài)添加的按鈕應(yīng)用相同的開關(guān)盒,像 buttonOnClick 你必須使用 implemented View 的 OnClickListerner's onClick.

Or you can apply same switch case for dynamically added buttons in your activity, like instead of buttonOnClick you have to use implemented View's OnClickListerner's onClick.

這篇關(guān)于Android 一個(gè)用于多個(gè)按鈕的 OnClick 方法?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久av网站 | 亚洲天码中字 | 涩涩在线 | 涩涩的视频 | 亚洲一级黄色片 | 日韩视频一区 | 丁香六月激情 | 中文毛片 | 国内自拍xxxx18 | 9999精品视频 | 欧美精品在线观看 | av福利在线观看 | 久久国产综合 | www午夜| 成人国产精品久久久网站 | 波多野结衣乳巨码无在线观看 | 免费a在线 | 亚洲精品久久久久久久久 | 中文字幕在线资源 | 日韩av在线免费 | 黄色大片av | 中文字幕理伦片免费看 | 黄色网免费 | 日韩毛片网 | 久久久久久久久久久久久久久久久 | 男女无遮挡xx00动态图120秒 | 免费av一区二区 | 亚洲欧美日韩综合 | 一级黄色片免费 | 日本黄色一级视频 | a级片免费观看 | 精品影院 | 青青草久久 | 中文字幕不卡在线 | 国产成人午夜高潮毛片 | 自拍偷拍中文字幕 | 国产主播精品 | 欧美久久一区二区 | 国产精品理论片 | 免费看黄色一级片 | 国产午夜av|