Basics: Setting up Android buttons and binding events

Source: Internet
Author: User

Before adding a new activity to the app, we used the button and clicked the button to enter a new activity. Here is a simple summary of the use of buttons on Android.

Design a button is very simple, in the IDE generally have a graphical interface, you can also write yourself in XML, such as: Macau casino play

View Source print?
1 <Button
2     android:id="@+id/nowamagicbtn"
3     android:layout_width="wrap_content"
4     android:layout_height="wrap_content"
5     android:text="前往PaintingActivity"/>

The next key is to give the button a couple of ways to bind the event.

The first type:

View Source print?
01 privateButton button;
02
03 @Override
04 protectedvoidonCreate(Bundle savedInstanceState) {
05     super.onCreate(savedInstanceState);
06     setContentView(R.layout.activity_main);
07    
08     button = (Button)findViewById(R.id.nowamagicbtn);
09
10     button.setOnClickListener(newOnClickListener(){
11
12         @Override
13         publicvoidonClick(View v) {
14             // TODO Auto-generated method stub
15             Intent intent = newIntent();
16             intent.setClass(MainActivity.this, PaintingActivity.class);
17             startActivity(intent);
18         }
19        
20     });
21 }

The second type:

View Source print?
01 privateButton button;
02
03 @Override
04 protectedvoidonCreate(Bundle savedInstanceState) {
05     super.onCreate(savedInstanceState);
06     setContentView(R.layout.activity_main);
07    
08     button = (Button)findViewById(R.id.nowamagicbtn);
09     button.setOnClickListener(newButtonListener());
10 }
11
12 classButtonListener implementsOnClickListener{
13
14     @Override
15     publicvoidonClick(View v) {
16         if( v.getId() == R.id.nowamagicbtn){
17             Intent intent = newIntent();
18             intent.setClass(MainActivity.this, PaintingActivity.class);
19             startActivity(intent);
20         }
21     }
22 }

The third type:

View Source print?
01 privateButton button;
02
03 @Override
04 protectedvoidonCreate(Bundle savedInstanceState) {
05     super.onCreate(savedInstanceState);
06     setContentView(R.layout.activity_main);
07    
08     button = (Button)findViewById(R.id.nowamagicbtn);
09     button.setOnClickListener(newButtonListener());
10 }
11
12 classButtonListener implementsOnClickListener{
13
14     @Override
15     publicvoidonClick(View v) {
16         // TODO Auto-generated method stub
17         Intent intent = newIntent();
18         intent.setClass(MainActivity.this, PaintingActivity.class);
19         startActivity(intent);
20     }
21 }

In fact, all are the same, are inherited Onclicklistener, and then rewrite its OnClick method. Summarize the convenience Yimeimei use.

Basics: Setting up Android buttons and binding events

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.