Android按钮setOnClickListener设计
我正在构建一个 Android 应用程序。我注意到我在每个类中创建了许多与此类似的重复代码:
Button buttonX = (Button)findViewById(R.id.buttonXName);
// Register the onClick listener with the implementation above
buttonX.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
}
});
我现在有十五个按钮,这使我的代码变得丑陋。有没有人有一个类或一些示例来说明如何将所有这些代码变成更有效的东西,这样我就可以:
- 创建按钮对象
{Button buttonX (Button)findViewById(R.id.buttonXName);}
- 设置监听器
{buttonX.setOnClickListener(new OnClickListener()}
- 判断是否被点击
{public void onClick(View v)}
- 然后为每个按钮运行特定代码?
如果有人知道任何事情,我将不胜感激。
I am building an Android Application. I've noticed that I am creating many repetitions of code similar to this in each of my classes:
Button buttonX = (Button)findViewById(R.id.buttonXName);
// Register the onClick listener with the implementation above
buttonX.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
}
});
I now have fifteen buttons and this is making my code ugly. Does anyone have a class or some examples on how I can turn all these codes into something more efficient, so I can:
- Create the button object
{Button buttonX (Button)findViewById(R.id.buttonXName);}
- Set the listener
{buttonX.setOnClickListener(new OnClickListener()}
- Determine if it was clicked
{public void onClick(View v)}
- Then run specific code for each button?
If anyone knows anything, I'd appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您的目标版本是 1.6 或更高版本,则可以使用 android: onClick xml属性去掉一些重复的代码。请参阅这篇博文罗曼·盖伊.
在 Java 类中,使用以下代码行:
If you're targeting 1.6 or later, you can use the android:onClick xml attribute to remove some of the repetitive code. See this blog post by Romain Guy.
And in the Java class, use these below lines of code:
在您的 Activity 上实现 OnClickListener()...
对于每个按钮使用...
在您的 Activity onClick() 方法中测试它是哪个按钮...
另外在 onClick 中您可以使用 view.getId() 来获取资源 ID 和然后在 switch/case 块中使用它来识别每个按钮并执行相关操作。
Implement OnClickListener() on your Activity...
For each button use...
In your Activity onClick() method test for which button it is...
Also in onClick you could use view.getId() to get the resource ID and then use that in a switch/case block to identify each button and perform the relevant action.
Android lambda 解决方案
Switch case 解决方案
Android lambada solution
Switch case solution solution
由于
setOnClickListener
是在View
而不是Button
上定义的,如果您不需要该变量用于其他用途,您可以使其更简洁,例如这:Since
setOnClickListener
is defined onView
notButton
, if you don't need the variable for something else, you could make it a little terser like this:您可以使用数组来处理 android 中的多个按钮单击侦听器,如下所示:
在这里,我使用数组为 n 个按钮设置按钮单击侦听器:
注意:n 是一个常量正整数
代码示例:
注意:首先编写一个 main.xml 文件,如果您不知道如何编写,请邮寄至:
[电子邮件受保护]
You can use array to handle several button click listener in android like this:
here i am setting button click listener for n buttons by using array as:
NOTE: n is a constant positive integer
Code example:
Note: First write an main.xml file if u dont know how to write please mail to:
[email protected]
我认为你通常可以在循环中完成你需要的事情,如果可以的话,这比许多
onClick
方法要好得多。查看这个答案,了解如何使用循环来实现类似的效果问题。如何构造循环将取决于
onClick
函数的需求以及它们之间的相似程度。最终结果是重复代码少得多,更易于维护。I think you can usually do what you need in a loop, which is much better than many
onClick
methods if it can be done.Check out this answer for a demonstration of how to use a loop for a similar problem. How you construct your loop will depend on the needs of your
onClick
functions and how similar they are to one another. The end result is much less repetitive code that is easier to maintain.使用
View.OnClickListener
实现 Activity,如下所示。Implement Activity with
View.OnClickListener
like below.