I have the following function to save data in a database, where "SaveData" is the onClick method of my button, but I want it to disable the button after clicking 1 time on it, to avoid saving all the time, my function is this one and I thought it was done with the onclicklistener thing but I see that it's not...:
public void GuardarDatos(View view) {
int primero = Integer.parseInt(Primero1.getText().toString());
finalizar =(ImageButton) findViewById(R.id.Guardar) ;
BaseHelper baseHelper = new BaseHelper (this,"DEMODB",null,1);
SQLiteDatabase db = baseHelper.getWritableDatabase();
if(db!=null){
ContentValues registronuevo = new ContentValues();
registronuevo.put("Primero",primero);
long i = db.insert("Notas",null,registronuevo);
if ( i>0)
Toast.makeText(this,"Puntuación guardada",Toast.LENGTH_SHORT).show();
finalizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
onStop(); //Se supone que esto basta pero no. No funciona
}
});
}
}
UPDATE:
finalizar = (ImageButton) findViewById(R.id.Guardar);
finalizar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//ejecuta el método
GuardarDatos(); // --> Aquí me obliga a pasarle algo ,
// pone : The method GuardarDatos(View) in the type Tiempoacabadobebe is not applicable for the arguments ()
//Deshabilita el botón
finalizar.setEnabled(false);
}
});
Write this down:
I recommend you do it this way:
Where button is the imagebutton, yours is called finish .
Be careful with the findViewById, the one I have put is an example, I don't know what the id of your button will be, and here too, be careful, I don't know if you are using button or AppCompatButton, otherwise I remember that's what it's called
It is important to ensure that the reference of the button to which you will assign it
listener
is correct,for the click to work correctly and the instruction to disable the button on click.
Colleagues, I found this solution in a previous forum. And it is simpler than setEnable(true/false), since sometimes it is difficult to capture the view of the button that you want to enable.
Declaration of Variables:
And finally we put these 5 lines where we want to avoid the double click:
For example I put it inside the onClick method:
with this it is more than enough to be able to deactivate the click on a button. :D