Andoid button + intent (suitable for beginners)

Source: Internet
Author: User

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


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 <textview>... </textview> (used to display Hello World), and add

 <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="140dp"        android:text="Button" />
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:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Textddddddddddddddddd" /></LinearLayout>

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 = 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.

<Activity Android: Name = ". tt"> </activity>


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.