检查按钮存在于Android中

时间:2013-05-31 00:02:27

标签: android dynamic

再次需要一些建议。

我正在努力完成这个模块的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;
     }
}

attached is a screen shot to what I'm seeing

2 个答案:

答案 0 :(得分: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
    }

答案 1 :(得分:2)

编辑:这个怎么样?添加一个新的类字段成员(类变量,而不是方法的局部变量),指示按钮是否实际添加到布局中。

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