Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with buttons in android
    primarykey
    data
    text
    <p>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...</p> <p>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),</p> <blockquote> <p>public void onCreate(Bundle icicle) {</p> <pre><code> super.onCreate(icicle); setContentView(R.layout.main); SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); SetWordsBtn.setOnClickListener(this); </code></pre> <p>}</p> </blockquote> <p>which implements a onClick() like this:</p> <blockquote> <p>public void onClick(View view) {</p> <pre><code> startWords(); </code></pre> <p>}</p> </blockquote> <p>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:</p> <blockquote> <pre><code> SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn); SetWordsBtn.setOnClickListener(this); DelWordsBtn.setOnClickListener(this); </code></pre> </blockquote> <p>but what about the onClick() method? Does it automatically apply itself to both the buttons when i do this? </p> <p>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? </p> <p>I was thinking the answer could be something like this, but i dunno :</p> <blockquote> <pre><code> //Declarations SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn); DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn); SetWordsBtn.setOnClickListener(setWordsView); DelWordsBtn.setOnClickListener(delWordsView); //onClick Functions </code></pre> <p>public void onClick(View setWordsView) {</p> <pre><code> startWords(); </code></pre> <p>}</p> <p>public void onClick(View delWordsView) {</p> <pre><code> deleteWords(); </code></pre> <p>}</p> </blockquote> <p>So it would actually link the startWords() function to the SetWordsBtn, and deleteWords() to DelWordsBtn...</p> <p>Any clear cut explanation/form of help would be appreciated. Thanks in advance guys. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload