Un Toast è una notifica mostrata per un particolare intervallo di tempo che scompare dopo qualche istante.
Le sue apparizioni improvvise ricordando il modo in cui il pane salta fuori dai tostapane.
La classe Toast utilizza due metodi statici:
public static Toast makeText(Context context, CharSequence text, int duration);
public static Toast makeText(Context context,int resId,int duration);
Per specificare la durata che può essere poco o tanto si utilizzano le costanti:
LENGTH_SHORT
LENGTH_LONG
Ecco un esempio utilizzando una CheckBox che non è altro che un elenco di scelte con cui l’utente può effettuare selezioni multiple:
Codice Java:
package com.exsample.chekBox.activities;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.exsample.chekBox.R;
import android.view.*;
import android.widget.CompoundButton;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox check1, check2, check3, check4;
String text;
if(check1!=null){
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
text= (checked ? “checked” : “unchecked”);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
text=(checked ? “checked” : “unchecked”);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
text=(checked ? “checked” : “unchecked”);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
text=(checked ? “checked” : “unchecked”);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
}