安卓開發頁面跳轉

安卓開發頁面跳轉
盲人摸象,難述其形;庖丁解牛,瞭然於胸。作爲程序員,不能爲敲代碼而敲代碼,要知其然知其所以然,要像庖丁解牛那樣將程序的思想和結構瞭然於胸,而不能像盲人摸象那樣做一個沒有思想只知所觸的碼奴。記住:思想是程序員的靈魂!
注意:

  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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章