4

もう一度アドバイスが必要です。

最後のハードルで立ち往生していますが、私が取り組んでいるこのモジュールは 99% 完了しています。実行時に画面にボタンを動的に公開しています。このボタンを押すと、新しいアクティビティ ビューが表示されます。私はそれを完全に機能させました。

ただし、ビューをランダムに変更する別のボタンがあるため、それが理にかなっている場合は、効果的にリセットする必要があります。ボタン (動的ボタン) がクリックされるたびに、別のボタンがスタックに追加されます。事実上、画面を下にクリックするたびにボタンがあります。私のロジックは間違っていますか、それともボタンが毎回表示されるのをチェックして防ぐ方法はありますか? つまり、一度だけ...以下はコードです。

public void ButtonOnClick(View v) {


    Random rnd = new Random();
    int randomListIndex = rnd.nextInt(2);

    Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
    int firstRun = 0;

    switch (randomListIndex) {
        case 0:

            //get the image your going to muck with
            image = (ImageView) findViewById(R.id.cardImageView);
            //set the image with what it should be
            image.setImageResource(R.drawable.storm);
            //apply the transition effect so it looks correct
            image.startAnimation(myFadeInAnimation);

            button = (Button)findViewById(R.id.dynamicButton);
            button.setText("Need another question?");

                Button myButton = new Button(this);
                myButton.setText("Press Me");


                LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
                layout.addView(myButton);

                final Button myButton1 = myButton;
                myButton.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View arg0) {

                        String activityName = "Storm";
                        Intent intent = new Intent(Page1.this, Page2.class);
                        intent.putExtra(ACTIVITYNAME,activityName);
                        startActivity(intent);

                    }
                });

        break;
     }
}

添付されているのは、私が見ているもののスクリーンショットです

4

2 に答える 2

3

編集:これはどうですか?ボタンが実際にレイアウトに追加されたかどうかを示す新しいクラス フィールド メンバー (メソッドのローカル変数ではなく、クラス変数) を追加します。

private boolean buttonShown = false; /* Here changed */

public void ButtonOnClick(View v) {


    Random rnd = new Random();
    int randomListIndex = rnd.nextInt(2);

    Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
    int firstRun = 0;

    switch (randomListIndex) {
        case 0:

            //get the image your going to muck with
            image = (ImageView) findViewById(R.id.cardImageView);
            //set the image with what it should be
            image.setImageResource(R.drawable.storm);
            //apply the transition effect so it looks correct
            image.startAnimation(myFadeInAnimation);

            button = (Button)findViewById(R.id.dynamicButton);
            button.setText("Need another question?");

            if (buttonShown == false) {  /* Here changed */

                Button myButton = new Button(this);
                myButton.setText("Press Me");

                LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
                layout.addView(myButton);

                final Button myButton1 = myButton;
                myButton.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View arg0) {

                        String activityName = "Storm";
                        Intent intent = new Intent(Page1.this, Page2.class);
                        intent.putExtra(ACTIVITYNAME,activityName);
                        startActivity(intent);

                    }
                });

                buttonShown = true;  /* Here changed */
            }  /* Here changed */

        break;
     }
}

再度編集: ローカル変数 myButton の代わりに、クラス フィールド メンバー pressMeButton を使用しました。

private Button pressMeButton;

public void ButtonOnClick(View v) {


    Random rnd = new Random();
    int randomListIndex = rnd.nextInt(2);

    Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
    int firstRun = 0;

    switch (randomListIndex) {
        case 0:

            //get the image your going to muck with
            image = (ImageView) findViewById(R.id.cardImageView);
            //set the image with what it should be
            image.setImageResource(R.drawable.storm);
            //apply the transition effect so it looks correct
            image.startAnimation(myFadeInAnimation);

            button = (Button)findViewById(R.id.dynamicButton);
            button.setText("Need another question?");

            if (pressMeButton == null) {
                pressMeButton = new Button(this);
                pressMeButton.setText("Press Me");

                LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
                layout.addView(pressMeButton);
            }

            /* If the pressMeButton is already in the layout, all you need to do is just changing the onClickListener. */
            pressMeButton.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View arg0) {

                    String activityName = "Storm";
                    Intent intent = new Intent(Page1.this, Page2.class);
                    intent.putExtra(ACTIVITYNAME,activityName);
                    startActivity(intent);

                }
            });

        break;
     }
}
于 2013-05-31T00:50:59.747 に答える