简体   繁体   English

知道在Android中单击了哪个按钮

[英]Knowing which button was clicked in Android

I am making an app which will contain 81 buttons on the same layout. 我正在制作一个将在相同布局上包含81个按钮的应用程序。 They are all referred as an object I created called "Tile". 它们全都称为我创建的称为“ Tile”的对象。 Problem is those tiles are stored in an array, so I need to know which button was clicked in an int format to be able to call a tile ( tiles[??] ). 问题是这些图块存储在数组中,因此我需要知道以int格式单击了哪个按钮才能调用图块( tiles[??] )。 I am using an onClick(View v) method. 我正在使用onClick(View v)方法。 Also I have tried this: 我也试过这个:

Log.i("Tile", v.getId() + "was clicked")

The result it gave me in the logcat was a really long integer. 它在logcat中给我的结果是一个非常长的整数。

So how can I know which button was clicked in a number format? 那么如何知道数字格式单击了哪个按钮? And what is the relationship between the casual id ( R.id.tile1 ) and this long integer, as it could help because it is already a number? 临时id( R.id.tile1 )和这个长整数之间的关系是什么,因为它已经是一个数字,所以可能会有所帮助?

PS: I know I could use Switch to assign each id to an integer, but as they are 81, it will be a waste of time and will make the code very complicated. PS:我知道我可以使用Switch将每个id分配给一个整数,但是因为它们是81,这将浪费时间,并使代码非常复杂。

I hope you can use tag option. 希望您可以使用标签选项。 You can set the tag for a view by view.setTag(1) and then on click event you can get back the tag set using view.getTag(). 您可以通过view.setTag(1)设置视图的标签,然后在单击事件时可以使用view.getTag()获取标签集。

In android we can use the id of the button for getting the click event in on click listener. 在android中,我们可以使用按钮的ID来获取Click监听器中的click事件。
Say you have, 5 Buttons: Make sure your Activity/Fragment implement OnClickListener 假设您有5个按钮:确保您的Activity / Fragment实现OnClickListener

// in OnCreate //在OnCreate中

  Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
  mClickButton1.setOnClickListener(this);
  Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
  mClickButton2.setOnClickListener(this);
  Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
  mClickButton3.setOnClickListener(this);
  Button mClickButton4 = (Button)findViewById(R.id.clickButton4);
  mClickButton4.setOnClickListener(this);
  Button mClickButton5 = (Button)findViewById(R.id.clickButton5);
  mClickButton5.setOnClickListener(this);</br>

//after this you will override the onclick method from OnClickListener //在此之后,您将覆盖OnClickListener中的onclick方法

public void onClick(View v) {
  switch (v.getId()) {
      case  R.id.clickButton1: {
        // do something for button 1 click
        break;
      }
      case R.id.clickButton2: {
        // do something for button 2 click
        break;
      }

    //.... etc
   }
}

You can use button.setTag(position) to set position as tag. 您可以使用button.setTag(position)将位置设置为标签。

And when onclick called get that tag value. 并在调用onclick时获取该标记值。

public void onclick(View v){
  int position = Integer.parseInt(v.getTag().toString());
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定在Android中单击了哪个动态按钮? - How to determine which dynamic button was clicked in android? 如何检查使用Android单击的按钮? - How to check which button is Clicked with android? Android:确定在xml中未定义该按钮时单击了哪个按钮 - Android: Determine which button was clicked given that button not defined in xml 在Java中单击了哪个按钮? - Which button was clicked in Java? 帮助了解按下了哪个按钮编号 - Help with knowing which button number was pressed 如何判断点击了哪个按钮? - How to tell which button was clicked? 使用指向单个方法的按钮来区分单击了android按钮数组中的哪个android按钮 - Distinguishing which android button in an array of android buttons was clicked with buttons pointing to a single method Android JSON对象解析存储在数组列表中,每次单击按钮都会更改 - Android JSON object parse store in array list which will change each time button clicked 如何使一个按钮执行一项任务,该任务取决于Java android中单击了哪个先前的按钮? - How to make one button do a task that depends on which previous buttons has been clicked in Java android? 当我单击该按钮时,我的应用程序崩溃,这将带我进入Java,Android Studio上的另一个活动 - My app crashes when I clicked the button which will take me the another Activity on Java, Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM