目錄
前言:
1.建立項目自帶的acitvity_main.xml
2.自己在建一個activiy_z.xml
3.編寫activiy 系統自帶生成的相關java代碼
3.1介紹
4.新的activiy java代碼
4.1介紹
5.注冊到AndroidManifest.xml
5.1介紹
6.示範圖檔
7.實作總結
前言:
最近公司打算做先關android 方面的技術,然後招andirod 還挺貴,老闆打算讓我來搞搞,那就搞一搞,順便做些總結,從java背景轉型快速入手 android 的小政策。
andoird 官網位址 https://developer.android.google.cn/
1.建立項目自帶的acitvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF8F40"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! 001"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:onClick="tz"
android:text="跳轉"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
2.自己在建一個activiy_z.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! 002 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
3.編寫activiy 系統自帶生成的相關java代碼
package com.example.app01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.app02.MainActivity02;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//關聯activity_main.xml 中的 onclick
public void tz(View v){
startActivity(new Intent(MainActivity.this, MainActivity02.class));
}
}
3.1介紹
- 跳轉方法和xml中的onclick 相關聯
4.新的activiy java代碼
package com.example.app02;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.app01.R;
public class MainActivity02 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_z);
}
}
4.1介紹
- 通過R.layout.activity_z 實作和activity_z.xml 相關聯
5.注冊到AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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="com.example.app02.MainActivity02"
android:label="second" />
</application>
</manifest>
5.1介紹
- 通過android:name 連結到java檔案
6.示範圖檔
- 點選跳轉就會跳轉到第二個頁面中
7.實作總結
- 1.建立一個簡單的項目
- 2.建立activiy_z.xml
- 3.編寫管理activiy_z的java代碼
- 4.将java代碼注入到AndroidManifest.xml中
- 5.在activiy_main.xml 中加入一個按鈕
- 6.編寫這個按鈕的java代碼
- 7.在java代碼中執行跳轉的方法
- 9.打包執行測試
- ok