安卓开发页面跳转

安卓开发页面跳转
盲人摸象,难述其形;庖丁解牛,了然于胸。作为程序员,不能为敲代码而敲代码,要知其然知其所以然,要像庖丁解牛那样将程序的思想和结构了然于胸,而不能像盲人摸象那样做一个没有思想只知所触的码奴。记住:思想是程序员的灵魂!
注意:

  1. Android studio中的.xml文件不能使用大写字母。
  2. 要在文件清单中对activity文件进行注册。
  3. id的命名格式如下,不要忘记+号 android:id=”@+id/textView_hello”

好了,下面进入正题:
当运行程序时,系统第一个读取的文件是Android文件清单→AndroidManifest.xml
要想实现页面跳转:从MainActivity.java→NextActivity.java,首先要在文件清单AndroidManifest.xml上对MainActivity.java和NextActivity.java进行注册,而且顺序不能颠倒。

Java文件实现动作控制,layout文件夹下面的XML文件则是对页面的部署,MainActivity.java对应的布局文件为activity_main.xml,
在MainActivity.java文件中实现绑定的语句为setContentView(R.layout.activity_main);
同理,NextActivity.java对应的布局文件为next_activity.xml,
在NextActivity.java文件中实现绑定的语句为
setContentView(R.layout.next_activity);
AndroidManifest.xml和 layout中.xml文件中引用的text文件
放在values文件夹下的strings.xml中,引用的style风格放在values文件夹下的styles.xml中。

为实现跳转需要在MainActivity.java中实现按钮二的监听。
button_second.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,NextActivity.class);

            startActivity(intent);
        }
    });
AndroidManifest.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yu.test">

    <application
        android:allowBackup="true"
        android:icon="@drawable/qq"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NextActivity">

        </activity>
    </application>

</manifest>

MainActivity.java代码如下:

package com.example.yu.test;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import  android.widget.*;

public class MainActivity extends AppCompatActivity{
    private Button button_geng;
    private Button button_second;
    private TextView textView_hello;

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button_geng=(Button) findViewById(R.id.button_geng);
        button_second=(Button) findViewById(R.id.button_second);
        textView_hello=(TextView) findViewById(R.id.textView_hello);

        button_geng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView_hello.setText("谁是最可爱的人");
            }
        });

        button_second.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent intent=new Intent(MainActivity.this,NextActivity.class);

                startActivity(intent);
            }
        });

    }
}

NextActivity.java代码如下:

package com.example.yu.test;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import  android.widget.*;
/**
 * Created by yu on 2016/4/29.
 */
public class NextActivity extends AppCompatActivity{
    private Button button_geng;
    private Button button_second;
    private TextView textView_hello;

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.next_activity);

        button_geng=(Button) findViewById(R.id.b21);
        button_second=(Button) findViewById(R.id.b22);
        textView_hello=(TextView) findViewById(R.id.t);

        button_geng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView_hello.setText("谁是最可爱的人");
            }
        });

        button_second.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                textView_hello.setText("情情最可爱");
            }
        });

    }
}

activity_main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.yu.test.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello"
        android:textColor="#ff0000"
        android:textSize="30sp"
        android:id="@+id/textView_hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="#317eec"
        android:text="@string/bt1"
        android:id="@+id/button_geng"
        android:layout_below="@+id/textView_hello"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bt2"
        android:id="@+id/button_second"
        android:layout_below="@id/button_geng"
        android:layout_centerHorizontal="true"
        android:background="#ff00ff"
        android:layout_marginTop="22dp"
        android:onClick="onclick"/>
</RelativeLayout>

next_activity.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/h"
        android:textColor="#ff0000"
        android:textSize="30sp"
        android:id="@+id/t"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="#317eec"
        android:text="@string/b1"
        android:id="@+id/b21"
        android:layout_below="@+id/t"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b2"
        android:id="@+id/b22"
        android:layout_below="@id/b21"
        android:layout_centerHorizontal="true"
        android:background="#ff00ff"
        android:layout_marginTop="22dp" />

</RelativeLayout>

styles.xml代码如下:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

strings.xml代码如下:

<resources>
    <string name="app_name">QQ</string>
    <string name="hello">耿宇仿制QQ</string>
    <string name="bt1">耿宇</string>
    <string name="bt2">love</string>
    <string name="h">第二个页面</string>
    <string name="b1">按钮一</string>
    <string name="b2">按钮二</string>
</resources>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值