java 在android中获取按钮的ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6121247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:24:12  来源:igfitidea点击:

Get Id of Button in android

javaandroidbackgroundadapter

提问by artouiros

I have a Button Adapter, i make 9 buttons in a gridview, then i set id for each button. BUt how do i use a button in another class, example: i need to change background of button with id 5. Here's my code

我有一个按钮适配器,我在网格视图中制作了 9 个按钮,然后我为每个按钮设置了 id。但是我如何在另一个类中使用按钮,例如:我需要更改 id 为 5 的按钮的背景。这是我的代码

public class ButtonAdapter extends BaseAdapter {  
    static Button btn;  
    private Context mContext;  

    // Gets the context so it can be used later  
    public ButtonAdapter(Context c) {  
     mContext = c;  

    }  



    // Total number of things contained within the adapter  
    public int getCount() {  
     return somestringarray.length;  
    }  

     // Require for structure, not really used in my code.  
    public Object getItem(int position) {  
     return null;  
    }  

    // Require for structure, not really used in my code. Can  
    // be used to get the id of an item in the adapter for  
    // manual control.  
    public long getItemId(int position) {  
     return position;  
    }  

    public View getView(int position,  
                              View convertView, ViewGroup parent) {  

     if (convertView == null) {  
      // if it's not recycled, initialize some attributes  
      btn = new Button(mContext);  
      btn.setLayoutParams(new GridView.LayoutParams(85, 85));  
      btn.setPadding(8, 8, 8, 8);  
      btn.setOnClickListener(new MyOnClickListener(position)); 
      }  
     else {  
      btn = (Button) convertView;  
     }  

     btn.setText(somestringarray[position]);  
     // filenames is an array of strings  
     btn.setTextColor(Color.BLACK);  
     btn.setBackgroundResource(INTarraywithpictures[position]);  

     btn.setId(position);  //here i set Id

     return btn;  
    }  
   }  

回答by MByD

After calling setContentView, you can use Button b = (Button)findViewById(theButtonId);to get a reference to it.

调用 后setContentView,您可以使用Button b = (Button)findViewById(theButtonId);来获取对它的引用。

回答by Niranj Patel

you can use setTag(value) and getTag(value) instead of setId()...

您可以使用 setTag(value) 和 getTag(value) 而不是 setId() ...

for more info..go setTagand getTag

更多信息..去setTaggetTag

回答by star angel

if u want to access your button in another class just declare the button as final and static....and if u declare the button as public then u can access the button in another class by creating the object of the class which contains button.

如果你想在另一个类中访问你的按钮,只需将按钮声明为最终和静态......如果你将按钮声明为公共,那么你可以通过创建包含按钮的类的对象来访问另一个类中的按钮。