简体   繁体   English

如何在单击的按钮中获取按钮 ID?

[英]How to get a button Id in of a clicked button?

I have a couple of buttons I want to make a computation on, so I want to get the ID of the particular button clicked so I can perform an operation on.我有几个按钮要对其进行计算,因此我想获得单击的特定按钮的 ID,以便我可以对其执行操作。

I have configured the id for all my buttons in my XML file.我已经为我的 XML 文件中的所有按钮配置了 id。 I hope i can get help here.我希望我能在这里得到帮助。

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity"
     android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="15dp"
            android:textSize="45dp"
            android:gravity="center_horizontal"
            android:textColor="@color/colorPrimaryDark"
             android:id="@+id/displayCalc"
            android:text="0"></TextView>

          <Button
             android:id="@+id/button_one"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="1"
             android:onClick="one"/>

         <Button
             android:id="@+id/button_two"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="2"
             android:onClick="two"/>

         <Button
             android:id="@+id/button_three"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="3"
             android:onClick="three"/>

         <Button
             android:id="@+id/button_add"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:text="+"
             android:textColor="@color/design_default_color_primary_dark"
             android:textSize="18sp"
             android:onClick="calcAdd"/>

     </LinearLayout>

Your question is a little strange.你的问题有点奇怪。 If I got it correctly you want to know which button is clicked in Java/Kotlin and perform some operation based on buuton id.如果我理解正确,您想知道在 Java/Kotlin 中单击了哪个按钮并根据按钮 ID 执行一些操作。 If that is what you want:如果这就是你想要的:

  1. Implement View.OnClickListener in your activity or fragment related to this xml.在与此 xml 相关的活动或片段中实现View.OnClickListener
  2. Implement onCLick(View) method.实现onCLick(View)方法。
  3. Now you can use when (kotlin) or switch (java) to find the clicked button id.现在您可以使用when (kotlin) 或switch (java) 来查找单击的按钮 id。
class MainActivity : AppCompatActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button_one.setOnClickListener(this)
        button_two.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.button_one -> {
                //TODO (do something for button with id button_one)
            }
            R.id.button_two -> {
                //TODO (do something for button with id button_two)
            }
        }
    }
}

say you're doing something like that说你正在做这样的事情

Button buttonOne = findViewById(R.id.button_one); //now you have access to the button that is in your layout
buttonOne.setOnClickLister(new View.OnClickListener(){ // i hope you understand anonymous inner class
@Override 
public void onClick(View v){ //now when your button is clicked this method is called 
//and you  passed v in argument which is nothing but your button
//you're passed a View object and not a out of the box button because the 
//View.OnClickListener can be implemented on any view if you want to call button 
//specific method on it you cast it to a button , because you know it is a button 
//inside
v.getId() //gives you the id of your button which you wanted
}
}

Tell me there is still some confusion .告诉我还有一些困惑。

You have defined in the XML file for the Button with id button_one with this attribute:您已在 XML 文件中为 ID 为button_oneButton定义了此属性:

android:onClick="one"

that the listener will be a method named one , so all you have to do is create that method inside your activity like this:侦听器将是一个名为one的方法,因此您要做的就是在您的活动中创建该方法,如下所示:

public void one(View view) {
    <your code goes here>
}

If you want to refer to the Button inside that method you can do it like this:如果要引用该方法中的Button ,可以这样做:

Button button = (Button) view;

and if you want its id:如果你想要它的 id:

int id = button.getId();

So create 4 methods (like the one above) inside your Activity class for all 4 buttons: one() , two() , three() and calcAdd() .因此,在您的 Activity 类中为所有 4 个按钮创建 4 个方法(如上面的一个): one()two()three()calcAdd()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM