欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android studio實現兩個界面間的切換

 更新時間:2022年04月23日 15:51:25   作者:Run  
這篇文章主要為大家詳細介紹了Android studio實現兩個界面間的切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android studio設計兩個界面間的切換,供大家參考,具體內容如下

實現兩個界面間的切換有兩種方式,第一種是xml間的相互切換,另外一種是兩個Activity間的切換。

范例:用兩種不同方法實現如圖功能,點擊button從第一頁切換至第二頁。

方案一 xml間的切換采用實現匿名內部類方式實現,這種方法適合只希望對監(jiān)聽器一次性使用,在該代碼塊運行完成之后,該監(jiān)聽器就不復存在。

step1:新建一個工程File-New-New Project

step2:接下來一路next,最后finish。

step3:工程新建完成后,在左側欄里依次展開app-java-第一個-MainActivity。在此編寫Java程序

package com.example.interaction;

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

public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);//首先調用xml中activity_main
? ? ? ? Button ok=(Button)this.findViewById(R.id.btn);
? ? ? ? //實現匿名內部類??
? ? ? ? ok.setOnClickListener(new View.OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v){
? ? ? ? ? ? ? ? setContentView(R.layout.twolayout);
? ? ? ? ? ? }
? ? ? ? });
? ? }}

step4:在左側欄依次展開app-res-layout-activity_main.xml,在此編寫第一個xml程序。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@+id/activity_main"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical"
? ? tools:context="com.example.interaction.MainActivity">
? ? <!--添加一個文本,顯示“第一頁”,第一二行必有,三四行上下邊距,第五行顯示文字,第六行水平居中-->
? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:layout_marginBottom="60dp"
? ? ? ? android:text="第一頁"
? ? ? ? android:layout_gravity="center_horizontal"/>
? ? <!--添加一個按鈕,顯示“第一頁”,第一二行必有,第三行居中對齊,第四行設置Button的id,第五行顯示文字-->
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_gravity="center"
? ? ? ? android:id="@+id/btn"
? ? ? ? android:text="切換頁面"/>
</LinearLayout>

step5:在已有工程中添加一個twolayout,依次點擊File-New-XML-Layout XML File,命名為twoLayout

step6:編寫程序

<?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:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginTop="30dp"
? ? ? ? android:layout_marginBottom="60dp"
? ? ? ? android:text="第二頁"
? ? ? ? android:layout_gravity="center_horizontal"/>
</LinearLayout>

方案二 兩個activity間的切換,該方法通過在Activity中定義一個內部類就繼承監(jiān)聽器接口

step1:新建一個工程File-New-New Project,與方案一中操作相同。

step2:接下來一路next,最后finish,與方案一中操作相同。

step3:新建一個.java文件,File-New-Java Class

命名為TwoActivity,點擊OK。

step4:在MainActivity中編寫程序

package com.example.interaction;

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

public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);//調用xml中activity_main。
? ? ? ? Button ok = (Button) this.findViewById(R.id.btn);
? ? ? ? ok.setOnClickListener(new ButtonListener());
? ? }
? ? private class ButtonListener implements View.OnClickListener{
? ? ? ? @Override
? ? ? ? public void onClick(View v){
? ? ? ? ? ? Intent intent=new Intent(MainActivity.this,TwoActivity.class);//設置切換對應activity
? ? ? ? ? ? startActivity(intent);//開始切換
? ? ? ? ? ? }
? ? }
}

step5:在TwoActivity中編寫程序

package com.example.interaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class TwoActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.twolayout);//調用twolayout
}}

step6:在左側欄中依次點擊app-manifests,添加一行代碼,添加位置如圖所示,必須在內,在外。

<activity android:name=".TwoActivity"></activity>

界面切換的兩種方式已經完成。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android屬性動畫實現圖片從左到右逐漸消失

    Android屬性動畫實現圖片從左到右逐漸消失

    這篇文章主要介紹了Android屬性動畫實現圖片從左到右逐漸消失,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android OKHTTP的單例和再封裝的實例

    Android OKHTTP的單例和再封裝的實例

    這篇文章主要介紹了Android OKHTTP的單例和再封裝的實例的相關資料,這里對OKHTTP的單例進行了分析總結,需要的朋友可以參考下
    2017-07-07
  • android中Webview實現截屏三種方式小結

    android中Webview實現截屏三種方式小結

    本篇文章主要介紹了android Webview實現截屏,主要詳解了3種方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android開發(fā)準確獲取手機IP地址的兩種方式

    Android開發(fā)準確獲取手機IP地址的兩種方式

    這篇文章主要介紹了Android開發(fā)準確獲取手機IP地址的兩種方式,需要的朋友可以參考下
    2020-03-03
  • Flutter路由傳遞參數及解析實現

    Flutter路由傳遞參數及解析實現

    這篇文章介紹了Flutter路由傳遞參數及解析實現的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • Android listview點贊問題分析

    Android listview點贊問題分析

    這篇文章主要為大家詳細解析了Android listview點贊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實現圖片點擊放大

    Android實現圖片點擊放大

    這篇文章主要為大家詳細介紹了Android實現圖片點擊放大,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android面試筆記之常問的Context

    Android面試筆記之常問的Context

    Android技術面試確實常常被問到Context,大概問題就是說說你對Context的理解吧,當時腦袋里浮現了是原來看到的文章片段亂說一通,這樣還是不行的。平時還是多積累知識,深刻理解Context,在項目開發(fā)過程中也能避免一些陷入坑中。下面就來看看個人的一些總結吧。
    2016-12-12
  • Android遍歷所有文件夾和子目錄搜索文件

    Android遍歷所有文件夾和子目錄搜索文件

    為了準確搜索文件,大家可以采取什么方法查找文件,本文為大家介紹Android遍歷所有文件夾和子目錄實現文件搜索功能,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android Tiny集成圖片壓縮框架的使用

    Android Tiny集成圖片壓縮框架的使用

    本篇文章主要介紹了Android Tiny集成圖片壓縮框架的使用,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論