Android的 - 处理网格 [英] Android - Handling a grid

查看:108
本文介绍了Android的 - 处理网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让按钮的2x2的网格和处理。现在我有四个按钮相对的观点活动......但我的问题是:是否做到这一点的最好方法是什么?不是给每个按钮的监听器?或者,反正是有按钮添加到GridView和处理它们都在同一个方法是什么?
例:
相反,使用类似的
如果(button1x1)...
如果(button1x2)...
如果(button2x2)...
如果(button2x1)...
和写入所有这些方法,是有我的方式只是一种方法,它会自动检测哪个按钮被推?很抱歉,如果这是一个令人困惑的问题,我可以认为它完美,但翻译成的话是有点难度。感谢您的帮助!

I am trying to make a 2x2 grid of buttons and handle them. Right now I have a relative view activity with four buttons...but my question is: is the best way to do this? Than give each button a listener? Or is there anyway to add the buttons to the GridView and handle them all in one method? Ex.: Instead of using something like if(button1x1)... if(button1x2)... if(button2x2)... if(button2x1)... and write a method for all of them, is there a way for me to just have one method and it will automatically detect which button is being pushed? Sorry if this is a confusing question, I can think it perfectly but translating to words is a bit difficult. Thanks for any help!

推荐答案

你首先你可以做

public class YourActivity extends Activity implements OnClickListener {...

,然后实施的onClick 方法

@Override
public void onClick(View view) {
   switch(view.getId()){
   case R.id.A_UI_Element:
      //do what you need for this element
      break:
   case R.id.A_Different_UI_Element:
      //do what you need for this element
      break;
   //continue with cases for each element you want to be clickable
   }
}

这篇关于Android的 - 处理网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆