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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...