¿Cómo desactivar el botón de clic?

En mi aplicación Android, hay un número de imágenes en la carpeta dibujable. En mi diseño hay dos botones: Botones Atrás y Siguiente. Al hacer clic en los botones siguiente y posterior 2 imágenes diferentes se cargan en el mismo diseño (común para todas las imágenes).

Problema : Puedo cargar imágenes en el botón Next / Back del botón, pero después de llegar a la última imagen, quiero hacer que mi botón Next deshabilitar y lo mismo para el botón de vuelta.Como el usuario está en la primera imagen el botón de volver debe estar desactivado . El código es como:

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

 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; 
 next.setClickable(false); 

La solución más preferida es,

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

Como un enlace puede ser ignorado fácilmente, tuve que repetir esto una y otra vez

Sólo tiene que insertar dos imágenes adicionales en su carpeta desplegable, una para la flecha derecha deshabilitada y otra para la flecha izquierda deshabilitada.

Ahora intenta esto

  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; 
  • Casting to Button es redundante - ¿Por qué?
  • ListView no responde a los eventos de clics en Android
  • Android Button setOnClickListener Design Ayuda
  • Android: la altura del botón no cambia
  • ¿Cómo manejar el botón Atrás en el diálogo?
  • ¿Por qué un botón pierde su comportamiento original después de cambiar su color?
  • Android Programar el texto del botón mediante programación
  • Barra de acción de Android con dos botones estirados
  • Cambiar el color del botón de Android en onClick?
  • Múltiples líneas de texto en un botón. Cada línea tiene diferentes estilos de texto
  • Color del borde del botón de Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.