Andoid Button + Intent (suitable for beginners)

Source: Internet
Author: User

Andoid Button + Intent (suitable for beginners)

When I first learned Android, I spent a lot of time on it and did not find much information. To learn other people's "key code", write it by yourself. I hope that later students will spend less time on it.

In fact, it is very simple to use an Android Intent to implement the page jump of the Button. click the Button in figure 1 to enter figure 2.

Figure 1 <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140811/201408111019032.jpg" width = "100" height = "150" alt = "\">

Figure 2

First, create an Android Application project. The main class name is MainActivity. If the sdk is okay, the system will automatically generate the onCreate function. If the MainActivity inherits the Activity, it will import

There will be a Hello World! There is an xml file in the res layout folder. The lower version is main. xml, and the higher version is activity_main.xml,

Used to define the layout. .. Delete related items (used to display Hello World), add

 
In Graphical layout, You can first see the defined page, 1, and then write the layout of Figure 2. Create an e. xml file under the original directory layout and the layout is LinearLayout. The content is as follows:
 
     
  
 

Write an tt class corresponding to Figure 2

package com.example.buttontest;import android.app.Activity;import android.os.Bundle;public class tt extends Activity{protected void onCreate(Bundle savedInstanceState)     {       super.onCreate(savedInstanceState);       setContentView(R.layout.e);    }}


The MainActivity content is as follows:

Package com. example. buttontest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class MainActivity extends Activity {Button but = null; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); but = (Button) findViewById (R. id. button1);. setOnClickListener (new View. onClickListener () {// listen to Buttonpublic void onClick (View arg0) {Intent intent = new Intent (); intent. setClass (MainActivity. this, tt. class); startActivity (intent );}});}}

You can add MainActivity. this. finish (); at the end of The onClick function. In addition, exit the entire system on Figure 2. If no value is added, the figure 1 page is displayed after figure 2 exits.

Finally, define the tt class in AndroidMainfest. xml, or enter the figure 2 interface in the simulator. The main class is generally defined by the system itself when it is created.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.