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

再次编辑:我使用类字段成员 pressMeButton 而不是局部变量 myButton。

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 回答
2

您的代码很好,但它总是会在屏幕上添加一个按钮。您正在寻找的期望行为是什么?如果它是一个有条件的事情,那么您需要在 onclick 调用中考虑这一点。

编辑:

因此,您需要做的是为按钮创建某种标志或 id,并将其设置为按钮的 view.tag,如下所示:

Button b = new Button(context);
b.setTag(flagTag);

然后,当您想检查按钮是否存在时,您可以像这样检查它:

if((LinearLayout) findViewById(R.id.nextPageContainer))
   .findViewById(tagFlag)==null){

       Button b = new Button(context);
       b.setTag(flagTag);

    }else{
       //do nothing or something :p
    }
于 2013-05-31T00:40:00.130 回答