Alright, so i’ve been making great progress on the app i’m trying to create, but most of the tutorials that i’ve been learning from only showcase the wondrous feature of having only one active widget inside the application at a time…
The thing is, my application requires 2 or more buttons and that’s the part i’m partially stuck at. My code implements a "SetWordsBtn" shown below (everything else is declared),
public void onCreate(Bundle icicle) {
super.onCreate(icicle); setContentView(R.layout.main); SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); SetWordsBtn.setOnClickListener(this);}
which implements a onClick() like this:
public void onClick(View view) {
startWords();}
but what if i have another button that deletes the words such as "DelWordsBtn"? I was thinking i could declare both buttons simultaneously like this:
SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn); SetWordsBtn.setOnClickListener(this); DelWordsBtn.setOnClickListener(this);
but what about the onClick() method? Does it automatically apply itself to both the buttons when i do this?
How am i able to declare a seperate onClick from each other so it both does different stuff when i click on either one of them?
I was thinking the answer could be something like this, but i dunno :
//Declarations SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn); SetWordsBtn.setOnClickListener(setWordsView); DelWordsBtn.setOnClickListener(delWordsView); //onClick Functionspublic void onClick(View setWordsView) {
startWords();}
public void onClick(View delWordsView) {
deleteWords();}
So it would actually link the startWords() function to the SetWordsBtn, and deleteWords() to DelWordsBtn…
Any clear cut explanation/form of help would be appreciated. Thanks in advance guys. 🙂
The typical convention is to just switch off of the ID of the View that is clicked. For example: