1. 程式人生 > >在 Android Studio 新增一個activity並實現跳轉

在 Android Studio 新增一個activity並實現跳轉

Android Studio比較人性化,當你新增一個activity時,他會自動把相關資訊寫入AndroidManifest.xml檔案中,同時新增activity的佈局檔案到資源res->layout下面。

我這裡隨便取個名稱,叫TextViewActivity。這時layout下面自動建立的這個檔案叫做activity_text_view.xml,就是TextViewActivity所對應的layout。

順便說一句,根據建立activity的命名不同,自動建立的檔名可能各不相同。我是用refactor根據自己的喜好修改了檔名(改成了TestTextViewAcitivity)。使用refactor的好處就是,程式會自動查詢檔案的所有引用,並在相應的地方加以修改。如果你不在意檔名,大可不用理會這些。 

為了演示activity之間的跳轉結果,先完成一個佈局檔案,如下

<?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/ttv1"
android
:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/tt_string" android:textColor="#5500FF" android:textSize="32sp" android:padding="10dp" /> <TextView android:id="@+id/ttv2" android:layout_width="355dp" android:layout_height="wrap_content" android:maxLines="1"
android:ellipsize="end" android:padding="10dp" android:text="what are you going to do" android:textColor="#00FF55" android:textSize="32sp" /> <TextView android:id="@+id/ttv3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="More Info" android:drawableRight="@drawable/arrow_down" android:textSize="32sp" android:padding="10dp" /> </LinearLayout>

原始碼中給不同文字添加了些效果,預覽時的顯示效果是這樣的,

接下來我們處理TestTextViewActivity這個檔案,先給出原始碼

package com.spacesoftwares.myapplication2;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class TestTextViewActivity extends AppCompatActivity {

    private TextView mtv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
mtv1 = findViewById(R.id.ttv1);
mtv1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // set strike through style in the text
mtv1.getPaint().setAntiAlias(true); // get rid of the zigzag effect
}
}

可以看到,setContentView(R.layout.activity_text_view); 也就是說這個activity所引用的layout就是activity_text_view.xml。

這裡還定義了一個TextView mtv1, 他首先通過findViewById(R.id.ttv1);找到layout中對應的TextView的例項(也就是第一行要顯示的文字),然後添加了strike through的效果。這個我們後面會看到。 

完成這個基本的activity之後,接下來我們需要完成跳轉工作。在預設情況下,我們直接在MainActivity中新增跳轉程式碼即可,如下

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

    private Button mBtnTextView;  // define a text view button
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnTextView = findViewById(R.id.btnTextView1);  // get the button, it is in activity_main.xml
mBtnTextView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, TestTextViewActivity.class);
startActivity(intent);
}
        });
};
}

其相應的layout(activity_main.xml)如下,

<?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"
>
    <Button
android:id="@+id/btnTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TestTextView"
/>
</LinearLayout>

其中,關鍵的跳轉工作的由這小段程式碼完成,

mBtnTextView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, TestTextViewActivity.class);
startActivity(intent);
}
});

基本沒什麼需要解釋的,首先建立一個傳遞引數的Intent,然後startActivity啟動這個intent所聯絡起來的activity。

說明一下,Android中提供的Intent機制是用來協助應用間的互動與通訊的,更直觀地說,Intent可用於應用程式之間,也可用於應用程式內部的activity, service和broadcast receiver之間的互動。Intent(Activity1, Activity2)這種結構中,Intent相當於一個溝通的橋樑,類似於中介的角色。

 再順便說明一下這時碰到的怪問題,Android的camelCase的命名方式,mBtnTextView這個名稱我寫成mbtntextView時居然不能成功執行,也不知是程式其他地方沒寫對還是怎麼的,把所有命名都改回來最後居然OK了,後來問題無法復現,所以這裡備註一下原因不明,以後碰到相同的問題再來檢查。

編譯執行,最後的效果如下所示,

點選該按鈕,就會跳出下面所示的介面,

嗯,是不是很簡單?