我有一个应用程序,我将一些资源应用于按钮来修改它们的背景。
一切正常,但是当我的应用程序在onPause之后转到onResume时,我无法再设置背景了。
我有一组18个按钮,当我在onResume时,我打电话给:
for (int i = 0; i < max; i++) {
Button b = l.get(i);
b.setBackgroundResource(R.color.green);
}
在l中,我有从findViewById()
获得的所有按钮的列表。
这仅适用于for的最后一个元素,但不适用于其他元素。
有什么想法吗?
**编辑**
这是我用来填充数组的代码
btn_1 = (Button)findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
l.add(btn_1);
我的所有按钮都会重复此操作。
**第二次编辑**
public void onResume() {
btn_1 = (Button)findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
l = new ArrayList<Button>();
l.add(btn_1);
...
for (int i = 0; i < max; i++) {
Button b = l.get(i);
b.setBackgroundResource(R.color.green);
}
}
在onCreate()
中也是如此。
答案 0 :(得分:4)
尝试下面的d ..这样可以正常工作。
List<Button> mButtonList = new ArrayList<Button>();
onCreate() {
mButtonList.add((Button) findViewById(R.id.btn_1));
mButtonList.add((Button) findViewById(R.id.btn_2));
mButtonList.add((Button) findViewById(R.id.btn_3));
....
for(Button b : mButtonList) {
b.setOnClickListener(this);
}
}
onResume() {
for(Button b : mButtonList) {
b.setBackgroundResource(R.color.green);
}
}
答案 1 :(得分:2)
简单示例:
<强>活动强>
public class StackOverFlowActivity extends Activity {
private ArrayList<Button> myArray = new ArrayList<Button>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myArray.add((Button)findViewById(R.id.btn_1));
myArray.add((Button)findViewById(R.id.btn_2));
myArray.add((Button)findViewById(R.id.btn_3));
myArray.add((Button)findViewById(R.id.btn_4));
myArray.add((Button)findViewById(R.id.btn_5));
}
@Override
protected void onResume() {
super.onResume();
for (Button b : myArray) {
b.setBackgroundColor(getResources().getColor(R.color.blue));
}
}
}
XML主
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:id="@+id/btn_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 5" />
</LinearLayout>
我在Strings.xml中的颜色
<color name="blue">#0099CC</color>
答案 2 :(得分:1)
请改用:
for (int i = 0; i < max; i++) {
Button b = l.get(i);
b.setBackgroundColor(getResources().getColor(R.color.green));
}
如果max非常大,您可能希望在循环之前缓存getColor
结果。
编辑:
根据文档:setBackgroundResource仅与drawable ID一起使用,而setBackgroundColor似乎正是您所需要的,因此代码的另一部分可能效果不佳,也许循环,setBackgroundColor
的上一行在我的应用程序上运行良好。
答案 3 :(得分:1)
你说过:
“:btn_1的代码相同,只需将btn_1与btn_2,btn_3交换等等。”
如果是这样,每次尝试添加新按钮时,这些行都会重置数组。
l = new ArrayList<Button>();
l.add(btn_1);
P.S。你应该避免使用按钮阵列的手动创建 - 而是使用一些育儿布局,如LinearLayout,然后使用getChildAt(i)获取他的按钮子,它更干净。例如:
// Register all the buttons in the layout to the OnClickListener
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_buttons);
int childcount = lin.getChildCount();
for (int i=0; i < childcount; i++){
View v = lin.getChildAt(i);
v.setOnClickListener(this);
}
答案 4 :(得分:0)
试试这个 -
for (int i = 0; i < l.getChildCount(); i++)
{
Button b = (Button) l.getChildAt(i);
b.setBackgroundResource(R.color.green);
}
答案 5 :(得分:0)
试试这个。它为我工作
for (int i = 0; i < count; i++) {
btn_title[i] = new Button(ActivityName.this);
btn_title[i].setText(menu.menu_title[i]);
btn_title[i].setId(i);
btn_title[i].setTextColor(Color.BLACK);
btn_title[i].setBackgroundColor(Color.parseColor(dataxml
.getMenucolor()));
btn_title[i]
.setTextSize(Integer.parseInt(dataxml.getFontsize()));
btn_title[i].setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
btn_title[i].setSingleLine();
btn_title[i].setEllipsize(TruncateAt.END);
btn_title[i].setOnClickListener(new ButtonListner());
}
希望这会有所帮助
答案 6 :(得分:0)
为什么不扩展Button类,设置它的颜色,然后使用那个新类?
答案 7 :(得分:-1)
它显然无法工作,让我解释为什么它设置了按钮的最后一个值。
你正在使用一个按钮引用(无论你从(Button)findViewById(R.id.btn_1)
得到什么)
并且不合时宜地有一个按钮引用,你只需使用l.add(btn_1);
您需要做的是,为每个按钮创建一个新的按钮实例,并在适当的视图中膨胀。
这是
的小刻度for (int i==0 ; i < length; i++ ){
Button button = new Button(context);
collectionOfButton.add(button) ;
}
如果您尝试集成某些 xml layout 文件,请使用 inflate 来使用该按钮集合。