gpt4 book ai didi

java - 在android中获取按钮的ID

转载 作者:行者123 更新时间:2023-12-02 08:17:15 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 在android中获取按钮的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6121247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com