微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查Android中是否存在按钮

再次需要一些建议.

尽管我陷入了最后一个障碍,但我正在完成的本模块已完成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;
     }
}

解决方法:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐