2

ボタンにいくつかのリソースを適用して背景を変更するアプリケーションがあります。

すべて正常に動作しますが、アプリケーションが onPause の後に onResume に移行すると、背景を設定できなくなります。

18 個のボタンのセットがあり、onResume にいるときは次のように呼び出します。

for (int i = 0; i < max; i++) {
    Button b = l.get(i);
    b.setBackgroundResource(R.color.green);
}

li には、 から取得したすべてのボタンのリストがありますfindViewById()

これは for の最後の要素でのみ機能し、他の要素では機能しません。

何か案が?

** 編集 **

これは、配列を作成するために使用するコードです

btn_1 = (Button)findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
l.add(btn_1);

これはすべてのボタンで繰り返されます。

** 2 回目の編集 **

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()ます。

4

8 に答える 8

5

次の 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);
    }
}
于 2012-07-02T15:49:01.923 に答える
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>
于 2012-07-04T08:08:47.370 に答える
1

代わりにこれを使用してください:

for (int i = 0; i < max; i++) {
    Button b = l.get(i);
    b.setBackgroundColor(getResources().getColor(R.color.green));
}

max が非常に大きい場合はgetColor、ループの前に結果をキャッシュすることをお勧めします。

編集:

ドキュメントによると: setBackgroundResourceは描画可能な ID のみで使用され、setBackgroundColorはまさにあなたが必要とするものであるように思われるので、コードの別の部分がうまく機能していない可能性があります。おそらくループ、上記の行はsetBackgroundColor私のアプリ。

于 2012-06-29T09:01:03.060 に答える
1

あなたは言った:

": ... コードは btn_1 と同じです。btn_1 を btn_2、btn_3 などと交換するだけです。"

もしそうなら、新しいボタンを追加しようとするたびにこれらの行があなたの配列をリセットしているよりも.

l = new ArrayList<Button>();
l.add(btn_1);

PS ボタン配列を手動で作成することは避けてください。代わりに、LinearLayout などの Parenting Layout を使用してから、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);
    }
于 2012-07-03T12:11:10.190 に答える
0

Buttonクラスを拡張し、その色を設定してから、その新しいクラスを使用しないのはなぜですか?

于 2012-07-06T02:40:57.313 に答える
0

これを試して -

for (int i = 0; i < l.getChildCount(); i++) 
{
    Button b = (Button) l.getChildAt(i);
    b.setBackgroundResource(R.color.green);
}
于 2012-06-19T17:51:36.840 に答える
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());

        }

これが役立つことを願っています

于 2012-06-29T10:44:12.767 に答える
-1

ボタンの最後の値を設定した理由を説明させてください。あなたは1つのボタン参照(から得たものは何でも(Button)findViewById(R.id.btn_1))を使用していますが、タイムリーに1つのボタン参照があり、使用してその値を変更するだけです l.add(btn_1);

あなたがする必要があるのは、ボタンごとに新しいボタンインスタンスを作成し、適切なビューでそれを膨らませることです。

これがそのための小さな目盛りです

for (int i==0 ; i < length; i++ ){
    Button button  = new Button(context); 

    collectionOfButton.add(button) ;
}

いくつかのxmlレイアウトファイルに統合しようとする場合は、インフレートを使用してそのボタンコレクションを使用してください。

于 2012-06-29T10:02:02.623 に答える