在android中获取按钮的ID

发布于 2024-11-09 10:59:11 字数 1601 浏览 6 评论 0原文

我有一个按钮适配器,我在 gridview 中制作了 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;  
    }  
   }  

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

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;  
    }  
   }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℉服软 2024-11-16 10:59:11

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

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

↙温凉少女 2024-11-16 10:59:11

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

了解更多信息..go setTaggetTag

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

for more info..go setTag and getTag

浮萍、无处依 2024-11-16 10:59:11

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文