Android one OnClick method for multiple buttons?(Android 一个用于多个按钮的 OnClick 方法?)
问题描述
我在android中开始了一点程序,我在一个活动中有 3 个按钮.
I started program little bit in android, I have 3 buttons in a single activity.
我看到一些示例代码将相同的 OnClick
事件分配给所有按钮(即使它们执行完全不同的操作)并且在方法 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...
什么是更好的方法?一个onClick
方法和切换还是很多方法,每个按钮一个?
What is the better approach? one onClick
method and switching or a lot of methods, one for each button?
谢谢.
推荐答案
如果你想减少代码行数那么使用 View 的 OnClick() 和 switch 语句
如果你想单独处理所有点击(便于理解和维护代码)然后使用单独的所有按钮的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 中实现该方法.现在你有一个所有按钮的方法,并在该方法中区分具有 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>
现在在您的 Activity 中实现 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;
}
}
或者您可以为活动中动态添加的按钮应用相同的开关盒,像 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
.
这篇关于Android 一个用于多个按钮的 OnClick 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 一个用于多个按钮的 OnClick 方法?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01