Android?Studio實(shí)現(xiàn)簡單頁面跳轉(zhuǎn)的詳細(xì)教程
項(xiàng)目實(shí)現(xiàn):(實(shí)現(xiàn)Android Studio 基本有兩種實(shí)現(xiàn)方式:一種為.MainActivity跳轉(zhuǎn);第二種是Relatelayout布局跳轉(zhuǎn)。
這里著重介紹第一種:(首先需要建立兩個XML文件,進(jìn)行布局的相互的跳轉(zhuǎn),然后使用兩個JAVA進(jìn)行相關(guān)的的設(shè)計(jì)?。?/strong>
(左上角四個文件為此次建立的新文件夾)
首先設(shè)置Activity_main的文件設(shè)置:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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 = "@mipmap/bc" tools:context=".MainActivity"> <TextView android:id="@+id/one" android:layout_width="200dp" android:layout_height="100dp" android:layout_centerInParent="true" /> <Button android:id="@+id/button" android:layout_width="299dp" android:layout_height="63dp" android:layout_below="@+id/one" android:layout_centerHorizontal="true" android:layout_marginTop="87dp" android:text="你這背景太假了" tools:ignore="MissingConstraints" /> </RelativeLayout>
另一個頁面布局的設(shè)計(jì):
代碼設(shè)計(jì):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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 = "@mipmap/ck" tools:context=".MainActivity"> <TextView android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是第二個頁面!" android:textSize="25dp" android:textColor="#663399" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout>
然后是第一個JAVA代碼的設(shè)計(jì):
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取按鈕 Button button = (Button) findViewById(R.id.button); //按鈕進(jìn)行監(jiān)聽 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //監(jiān)聽按鈕,如果點(diǎn)擊,就跳轉(zhuǎn) Intent intent = new Intent(); //前一個(MainActivity.this)是目前頁面,后面一個是要跳轉(zhuǎn)的下一個頁面 intent.setClass(MainActivity.this,Sencond.class); startActivity(intent); } }); } }
另一個跳轉(zhuǎn)文件所需要的頁面JAVA代碼:
public class Sencond extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //這個是獲取布局文件的,這里是你下一個頁面的布局文件//注意這個是跳轉(zhuǎn)界面的不能設(shè)置錯,應(yīng)該是第一個 setContentView(R.layout.sencond); } }
最后一點(diǎn)著重說明一下項(xiàng)目實(shí)現(xiàn)的遇到的問題:
1.沒有注冊相關(guān)的文件名稱:
2.新建XML文件的時候的字符設(shè)計(jì)出現(xiàn)大寫錯誤(設(shè)置XML文件的時候注意了,別設(shè)置超過的大寫)
Error:Error: 'O' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore Error:Execution failed for task ':bluetooth:mergeDebugResources'. F:\Android_Studio_Project\BLE_APP\bluetooth\src\main\res\layout\automaticOpenLayout.xml: Error: 'O' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore F:\Android_Studio_Project\BLE_APP\bluetooth\src\main\res\layout\automaticOpenLayout.xml
3.遇到的第三個小問題(出現(xiàn)導(dǎo)入圖片文件錯位,主要設(shè)置背景圖片需要將圖片復(fù)制到相關(guān)的xhdpi文件當(dāng)中)
最終效果:
跳轉(zhuǎn)后的界面:
總結(jié)
到此這篇關(guān)于Android Studio實(shí)現(xiàn)簡單頁面跳轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Android Studio頁面跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中ScrollView實(shí)現(xiàn)滑動距離監(jiān)聽器的方法
ScrollView相信對每位Android開發(fā)者們來說都不陌生,所以這篇文章給大家主要介紹了Android中ScrollView實(shí)現(xiàn)滑動距離監(jiān)聽器的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10Android實(shí)現(xiàn)收到新短信后自動發(fā)郵件功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)收到新短信后自動發(fā)郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android?Studio實(shí)現(xiàn)帶三角函數(shù)對數(shù)運(yùn)算功能的高級計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)帶三角函數(shù)對數(shù)運(yùn)算功能的高級計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android 微信小視頻錄制功能實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Android 微信小視頻錄制功能實(shí)現(xiàn)詳解的相關(guān)資料,這里提供了具體的實(shí)現(xiàn)思路及代碼,需要的朋友可以參考下2016-11-11Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07如何利用Kotlin實(shí)現(xiàn)極簡回調(diào)
這篇文章主要給大家介紹了關(guān)于如何利用Kotlin實(shí)現(xiàn)極簡回調(diào)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Android搜索結(jié)果顯示高亮實(shí)例(有數(shù)據(jù)滑動底部自動刷新)
本篇文章主要介紹了Android搜索結(jié)果顯示高亮實(shí)例(有數(shù)據(jù)滑動底部自動刷新),非常具有實(shí)用價值,需要的朋友可以參考下2017-04-04MaterialApp?Flutter?應(yīng)用全局配置與主題管理詳解
這篇文章主要為大家介紹了MaterialApp?Flutter?應(yīng)用全局配置與主題管理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解
這篇文章主要介紹了Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11