如何禁用按钮单击?

时间:2010-11-17 11:43:18

标签: android button

在我的Android应用程序中,drawable文件夹中有许多图像。在我的布局中有两个按钮:后退和下一步按钮。在单击下一个和后退按钮时,2个不同的图像将加载到相同的布局上(对所有图像都是通用的)。

问题:我可以在下一个/上一个按钮的点击中加载图像但是在到达最后一个图像后,我想要使我的下一个按钮禁用,并且后退按钮也相同。因为用户是在第一张图像上,应禁用后退按钮。代码如下:

public class SequencerActivity extends Activity implements OnClickListener
    {
        private int imageCounter = 0;
        private ImageView imageLoader;

        private int[] imageList = {R.drawable.image_wo_lbl_0, R.drawable.image_wo_lbl_1, R.drawable.image_wo_lbl_2, R.drawable.image_wo_lbl_3, R.drawable.image_wo_lbl_4, R.drawable.image_wo_lbl_5,
                R.drawable.image_wo_lbl_6, R.drawable.image_wo_lbl_8, R.drawable.image_wo_lbl_9,R.drawable.image_wo_lbl_10, R.drawable.image_wo_lbl_11};
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            setContentView(R.layout.parent_frame);//this one is the common parent layout for all image views
            super.onCreate(savedInstanceState);

            /*requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);*/

            //int image1 = R.drawable.image_w_lbl_0;

            imageLoader = (ImageView) findViewById(R.id.imageLoader);
            //imageLoader.setImageResource(image1);

            ImageButton next = (ImageButton) findViewById(R.id.next);
            ImageButton back = (ImageButton) findViewById(R.id.back);
            next.setOnClickListener(this);
            back.setOnClickListener(this);
            //show the default image
            this.loadImage(imageList[imageCounter]);

        }
        @Override
        public void onClick(View v) 
        {
            int imagePath = 0;
            // TODO Auto-generated method stub
            switch (v.getId())
            {
            case R.id.next:
                Log.i("Tag","tag");
                if(imageCounter < imageList.length)
                {
                    imageCounter++;
                    imagePath = imageList[imageCounter];
                    if (imageCounter==(imageList.length)-1)
                    {
                        //how to make my next button disable

                    }
                }
                break;
            case R.id.back:
                if(imageCounter > 0)
                {
                    imageCounter--;
                    imagePath = imageList[imageCounter];
                    if (imageCounter==0)
                    {
                        //how to make my back button disable
                    }
                }
                break;
            }
            this.loadImage(imagePath);

        }

        private void loadImage(int imagePath)
        {
            imageLoader.setImageResource(imagePath);

        }
    }

5 个答案:

答案 0 :(得分:17)

case R.id.next:
        Log.i("Tag","tag");
        if(imageCounter < imageList.length)
        {
            imageCounter++;
            imagePath = imageList[imageCounter];
            if (imageCounter==(imageList.length)-1)
            {
                ImageButton next=(ImageButton)findViewBYId(R.id.next);
                next.setEnabled(false);
            }
        }
        break;
    case R.id.back:
        if(imageCounter > 0)
        {
            imageCounter--;
            imagePath = imageList[imageCounter];
            if (imageCounter==0)
            {
                ImageButton back=(ImageButton)findViewBYId(r.id.back);
                back.setEnabled(false);
            }
        }
        break;

答案 1 :(得分:9)

next.setClickable(false);

答案 2 :(得分:4)

更优选的解决方案是,

onclick(){
  btn.setEnabled(false);
  btn.setClickable(false);
  //yourwork
  myWork();
}

myWork(){
 //your tasks.
 btn.setEnabled(true);
 btn.setClickable(true);
}

由于链接可以轻易忽略,我不得不一次又一次地重复这个

答案 3 :(得分:1)

使用kotlin,您可以 禁用按钮

button.isEnabled = false

或禁用 点击

button.isClickable = false

答案 4 :(得分:0)

只需在可绘制文件夹中插入两个额外图像,一个用于禁用右箭头,另一个用于禁用左箭头。

现在试试这个

         case R.id.next:

         Log.i("Tag","tag");

            if(imageCounter < imageList.length)


            {
                imageCounter++;

                imagePath = imageList[imageCounter];

                if (imageCounter==(imageList.length)-1)

                {


                      //disabling right button by changing image from following code                                                                                                                

                      next.setImageDrawable(getResources().getDrawable(R.drawable.right_disabled));

                }
            }
            break;
        case R.id.back:

            if(imageCounter > 0)

            {
                imageCounter--;

                imagePath = imageList[imageCounter];

                if (imageCounter==0)

                {
                      back.setImageDrawable(getResources().getDrawable(R.drawable.left_disabled));
                }
            }
            break;