Go: two ways to respond to button in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>1.</strong></p></p> <p><p><strong>Add a listener name to the button in the layout file</strong></p></p> <p><p><strong>android:onclick= "buttononclick"</strong></p></p> <p><p><strong>For example:</strong></p></p> <p><p><button</p></p> <p><p>android:id=<em>"@+id/button2"</em></p></p> <p><p>Android:layout_width=<em>"wrap_content"</em></p></p> <p><p>android:layout_height=<em>"wrap_content"</em></p></p> <p><p>android:layout_alignright=<em>"@+id/button1"</em></p></p> <p><p>android:layout_below=<em>"@+id/textview2"</em></p></p> <p><p>android:layout_margintop=<em>"28dp"</em></p></p> <p><p><u>android:text=<em>"button"</em></u></p></p> <p><p>android:onclick=<em>"buttononclick" </em>/><strong></strong></p></p> <p><p><strong></strong></p></p> <p><p><strong>function to create a response in activity</strong></p></p> <p><p><strong>publicvoid</strong> Buttononclick (view view) {</p></p> <p><p>Mytext1.settext ("hello!"); /set the display of the document</p></p> <p><p>}</p></p> <p><p><strong>2.</strong></p></p> <p><p><strong>Write in the OnCreate function</strong></p></p> <p><p><strong>Mybutton = (Button) Findviewbyid (r.id. <em> Button1</em>);</strong></p></p> <p><p>Mybutton.setonclicklistener (<strong>new</strong> onclicklistener () {</p></p> <p><p>@Override</p></p> <p><p><strong>publicvoid</strong> OnClick (View v) {</p></p> <p><p><strong>TODO</strong> auto-generated Method stub</p></p> <p><p>Mytext.settext ("hello!"); /set the display of the document</p></p> <p><p>}</p></p> <p><p>});</p></p> <p><p>3. <strong>write in the OnCreate function</strong></p></p> <p><p>Findviewbyid (r.id. <em> Btn_title_popmenu</em>). setonclicklistener (<strong></strong>this);</p></p> <p><p>Findviewbyid (r.id. <em> Button1</em>). Setonclicklistener (<strong></strong>this);</p></p> <p><p>Again</p></p> <p><p><strong>publicvoid</strong> OnClick (View v) {</p></p> <p><p><strong>if</strong> (v.getid () = = R.id. <em>Btn_title_popmenu</em>) {</p></p> <p><p>Popmenu.showasdropdown (v);</p></p> <p><p>}<strong>ElseIf</strong>(v.getid () ==r.id. <em> Button1</em>)</p></p> <p><p>{</p></p> <p><p>Popmenu1.showasdropdown (v);</p></p> <p><p>}</p></p> <p><p>}</p></p> <p><p>The Prerequisites are:</p></p> <p><p><strong></strong> public <strong>class</strong> Mainactivity <strong>extends</strong> Activity <strong>implements</strong> onclicklistener{}</p></p> <p><p>An example of the third method:</p></p> <p><p><strong>Publicclass</strong> Mainactivity <strong>extends</strong> Activity <strong>implements</strong> onclicklistener{</p></p> <p><p><strong>Private</strong> TextView Mytext = <strong>null</strong>;</p></p> <p><p><strong>Private</strong> TextView MYTEXT1 = <strong>null</strong>;</p></p> <p><p>@Override</p></p> <p><p>Run when activity is run for the first time</p></p> <p><p><strong>protectedvoid</strong> onCreate (Bundle Savedinstancestate) {</p></p> <p><p><strong>Super</strong>. OnCreate (savedinstancestate);</p></p> <p><p>Setcontentview (r.layout. <em> Activity_main</em>);//set The control to be displayed</p></p> <p><p>Findviewbyid (r.id. <em> Button1</em>). Setonclicklistener (<strong></strong>this);</p></p> <p><p>Findviewbyid (r.id. <em> Button2</em>). setonclicklistener (<strong></strong>this);</p></p> <p><p>Mytext = (TextView) Findviewbyid (r.id. <em> TextView1</em>);</p></p> <p><p>Mytext1 = (TextView) Findviewbyid (r.id. <em> TextView2</em>);</p></p> <p><p>}</p></p> <p><p>@Override</p></p> <p><p><strong>publicvoid</strong> OnClick (View arg0) {</p></p> <p><p><strong>TODO</strong> auto-generated method stubs</p></p> <p><p><strong>if</strong> (arg0.getid () ==r.id. <em>button1</em>) {</p></p> <p><p>Mytext.settext ("hello!"); /set the display of the document</p></p> <p><p>}</p></p> <p><p><strong>ElseIf</strong> (arg0.getid () ==r.id. <em>button2</em>) {</p></p> <p><p>Mytext1.settext ("hello!"); /set the display of the document</p></p> <p><p>}</p></p> <p><p>}</p></p> <p><p>Examples of the first two methods:</p></p> <p><p><strong>public class Mainactivity extends Activity {</strong></p></p> <p><p><strong>Private Button Mybutton = null;</strong></p></p> <p><p><strong>Private Button Mybutton1 = null;</strong></p></p> <p><p><strong>Private TextView Mytext = null;</strong></p></p> <p><p><strong>Private TextView Mytext1 = null;</strong></p></p> <p><p><strong>@Override</strong></p></p> <p><p><strong>Run when activity is run for the first time</strong></p></p> <p><p><strong>protected void OnCreate (Bundle Savedinstancestate) {</strong></p></p> <p><p><strong>Super.oncreate (savedinstancestate);</strong></p></p> <p><p><strong>Setcontentview (r.layout.activity_main);//set the control to display</strong></p></p> <p><p><strong>Mybutton = (Button) Findviewbyid (r.id.button1);</strong></p></p> <p><p><strong>Mytext = (TextView) Findviewbyid (r.id.textview1);</strong></p></p> <p><p><strong>Mytext1 = (TextView) Findviewbyid (r.id.textview2);</strong></p></p> <p><p><strong>The second method of</strong></p></p> <p><p><strong>Mybutton.setonclicklistener (new Onclicklistener () {</strong></p></p> <p><p><strong>@Override</strong></p></p> <p><p><strong>public void OnClick (View V) {</strong></p></p> <p><p><strong>TODO auto-generated Method Stub</strong></p></p> <p><p><strong>Mytext.settext ("hello!"); /set the display of the document</strong></p></p> <p><p><strong>}</strong></p></p> <p><p><strong>});</strong></p></p> <p><p><strong>}</strong></p></p> <p><p><strong>The first of these methods</strong></p></p> <p><p><strong>public void Button2onclick (view View) {</strong></p></p> <p><p><strong>Mytext1.settext ("hello!"); /set the display of the document</strong></p></p> <p><p><strong>}</strong></p></p> <p><p><strong>//first method to set</strong> android:onclick=<em>"buttononclick"</em> in the Button's layout file</p></p><p><p>Go: two ways to respond to button in Android</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.