Android Development Basics Small Notes, add button events, open new form, form pass value, callback

Source: Internet
Author: User

Add an onclick event to a button

        Get Button Object
Button Aiyo = (Button) Findviewbyid (r.id.button1); Aiyo.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View v) {///tv.settext ("Woceshi"); //pop-up Tips toast.maketext (getapplicationcontext (), ' Hello ', Toast.length_short). Show (); } );


Open a new window (activity)

// Create a Form object New Intent (); Newwindow.setclass (mainactivity. this, NewWindow. class ); // The first is the current form class, and the second is a new form class (form name. Class) startactivity (NewWindow); // start a new form

The current form passes values to the new form

This is what the current form does

New Intent (); Newwindow.setclass (mainactivity. this, NewWindow. class ); // New Open Window pass value New bundle (); Bundle.putstring ("Bundlekey", "Zongwenlong"); Newwindow.putextras (bundle) ; // New open Window Pass value end
The code on the three lines above is actually a bit more complex, and can be written as follows
New Window Pass value 1
Newwindow.putextra ("Key", "value");
New Window value 1 endstartactivity (NewWindow);

What the new form does

Write in the oncreate of the new form

// get the value from the previous form  This . Getintent (). Getextras (); LOG.E ("Zllmsg", Bundle.getstring ("Bundlekey")); // gets the value that came from the previous    form end

The new window closes and then passes the value back to the old window

What the old window did

New Intent (); Newwindow.setclass (mainactivity. this, NewWindow. class ); // New Open Window pass value New bundle (); Bundle.putstring ("Bundlekey", "Zongwenlong"); Newwindow.putextras (bundle) ; // New open Window Pass value End // startactivity (NewWindow); Startactivityforresult (NewWindow, 1111);//This 1111 is a unique code, also need to use


new window to do things, write a button event

Newnew  Bundle (); Bundle.putstring ("Zllfanhui", "Zonglonglongfanhui"); Intent.putextras (bundle); Setresult (1111, intent); finish ();

Old window to do things again, to implement an interface

@Override     protected void onactivityresult (intint  resultcode, Intent data) {        //  TODO auto-generated method Stub        Super. Onactivityresult (Requestcode, ResultCode, data);        LOG.E ("Zllmsg", Data.getextras () getString ("Zllfanhui"));    

Android Development Basics Small Notes, add button events, open new form, form pass value, callback

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.