網站首頁 編程語言 正文
Android studio設計兩個界面間的切換,供大家參考,具體內容如下
實現兩個界面間的切換有兩種方式,第一種是xml間的相互切換,另外一種是兩個Activity間的切換。
范例:用兩種不同方法實現如圖功能,點擊button從第一頁切換至第二頁。
方案一?xml間的切換采用實現匿名內部類方式實現,這種方法適合只希望對監聽器一次性使用,在該代碼塊運行完成之后,該監聽器就不復存在。
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中定義一個內部類就繼承監聽器接口
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>
界面切換的兩種方式已經完成。
原文鏈接:https://blog.csdn.net/weixin_43230707/article/details/113970267
相關推薦
- 2022-12-31 Echars 報錯: Error in created hook: “Cannot read pro
- 2022-04-06 python實現一個搖骰子小游戲_python
- 2024-02-27 Go 讀取控制臺輸入
- 2022-07-17 使用非root用戶安裝及啟動docker的問題(rootless模式運行)_docker
- 2022-10-27 C++設計模式中的工廠模式詳細介紹_C 語言
- 2022-06-17 Go語言HttpRouter路由使用方法詳解_Golang
- 2022-07-29 Android錄音功能的實現以及踩坑實戰記錄_Android
- 2022-02-17 docker容器內的數據存放在哪里
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支