AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iii.lcpan.activity">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        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>
        注意不可以複製 <intent-filter>等等,那會使他產生兩個應用程式
    </application>

</manifest>



<resources>
    <string name="app_name">Activity</string>
    <string name="firstpage">第一頁</string>
    <string name="secondpage">第二頁</string>
    <string name="nextpage">下一頁</string>
    <string name="lastpage">上一頁</string>
</resources>



第一個畫面:
<?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.iii.lcpan.activity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/firstpage"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nextpage"
        android:id="@+id/button1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false" />
</RelativeLayout>

第二個畫面:
<?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.iii.lcpan.activity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/secondpage"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lastpage"
        android:id="@+id/button2"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false" />
</RelativeLayout>


第一頁轉第二頁的程式:

package com.iii.lcpan.activity;

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

public class MainActivity extends AppCompatActivity {//implements View.OnClickListener {<<<<因為用了匿名class,所以就不用implements了
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button1);
//        btn.setOnClickListener(this);<<<<不用匿名的話就事先連結MainActivity 到button

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(); //Intent(意圖),用以切換頁面,這個方式每用一次就會產生一個新的activity
                intent.setClass(MainActivity.this, NextActivity.class);//setClass(本頁,要去的那一頁.class)
                startActivity(intent);//真正行動了
            }
        });
    }
//    @Override //不用匿名方法的話
//    public void onClick(View v) {
//        Intent intent=new Intent();
//        intent.setClass(this,NextActivity.class);
//        startActivity(intent);
//    }
}



第二頁轉回第一頁的程式:

 

package com.iii.lcpan.activity;

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

/**
 * Created by Student on 2016/6/23.
 */
public class NextActivity extends AppCompatActivity {//implements View.OnClickListener
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);

        btn = (Button) findViewById(R.id.button2);
//        btn.setOnClickListener(this);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
               回到上一頁不需要再創造一個activity,所以直接用finish關掉第二頁就好,自然會回到第一頁去
            }
        });
    }
//    @Override
//    public void onClick(View v) {
////        Intent intent=new Intent();
////        intent.setClass(this,MainActivity.class);
////        startActivity(intent);
//        finish();
//    }
}
創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()