Comment coder un événement onclick de bouton ?
-
11-12-2019 - |
Question
But:Pour que l'instruction if exécute un événement onclick via du code.
J'ai actuellement ce bouton en XML.
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Call"
android:onClick="makeCall"/>
j'ai essayé R.id.button1.performClick();
mais je reçois
Impossible d'invoquer performClick() sur le type primitif int
J'ai le conditionnel ci-dessous à répéter si le nombre est inférieur à 5.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
int count=0;
if (count <= 5) {
//make call
R.id.button1.performClick();
count++;
Toast toast=Toast.makeText(this, "Count is currently" + count++ + ",
repeating", Toast.LENGTH_LONG);
toast.show();
}
else {
// Toast Popup when call set button pressed
Toast toast=Toast.makeText(this, "Call count complete, ending method" ,
Toast.LENGTH_LONG);
toast.show();
count++;
}
}
La solution
Vous devez obtenir une référence au bouton, vous pouvez ensuite l'utiliser.
Button button1 = yourview.findViewById(r.id.button1);
button1.performClick();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow