Reputation: 140
This is some of the current code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
If, for example, I wanted to print a number after the Button 'loseStarter1' is pressed, how would I do so?
This is something I'd like to learn that can be applied for any action after pressing a Button, such as starting an interactive Activity.
Also, would it be in the onCreate()
method?
Much appreciated, note that I am a beginner.
Upvotes: 1
Views: 1011
Reputation: 191728
Firstly, your code won't compile - You closed off the onCreate
method and findViewById
must be inside onCreate
and after setContentView
.
would it be in the onCreate method?
Not necessarily in that particular method, only after the view has been loaded.
With regards to your question. What exactly do you not understand about how a click event works? You define a button, and then set the action to be "performed after it is clicked".
Button loseStarter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(icicle);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: Perform actions here...
// action1();
// action2();
}
});
}
for example, I wanted to print a number after the button 'loseStarter1' is pressed, how would I do so?
Assuming game1
is the class name
Toast.makeText(game1.this, String.valueOf(42), Toast.LENGTH_SHORT).show();
Upvotes: 1