Android中多界面跳转的一个简单应用

2024-01-12 15:18

本文主要是介绍Android中多界面跳转的一个简单应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 多界面跳转的步骤:

  一,在layout布局中编辑好布局

  二,在src文件夹下写逻辑

  三,设置权限

  四,在清单文件下注册新建的Activity

1<activity android:name="com.example.jump.homeActivity" ></activity>

2<activity android:name="com.example.jump.phoneActivity"></activity>

3<activity android:name="com.example.jump.sdCardActivity"></activity>

4.<activity android:name="com.example.jump.smsActivity"></activity>

下面模拟了一个多界面跳转的实例,从启动页跳转到第二个界面,然后分别跳转到发短信的界面,打电话的界面,获取手机内存的界面。

布局和代码如下:

MainActivity布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

 

    tools:context=".MainActivity" >

 

    <ImageView

        android:src="@drawable/qidong2"

        android:scaleType="fitXY"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:text="@string/hello_world" />

    <Button 

        android:id="@+id/bt_start"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/start"

       android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"/>

 

</RelativeLayout>

 

第二个界面布局中:

<?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" 

    android:background="#44f0f0">

    <Button 

        android:id="@+id/bt_send"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/send"

        android:layout_marginTop="30dp"

        android:onClick="open01"

        />

      <Button 

        android:id="@+id/bt_call"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/call"

        android:layout_marginTop="30dp"

        android:onClick="open02"

        />  

        <Button 

        android:id="@+id/bt_save"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/get"

        android:layout_marginTop="30dp"

        android:onClick="open03"

        />

 

</LinearLayout>

 

发短信界面中:

<?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:background="#4400ff00"

    android:orientation="vertical" >

    <TextView 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="请输入收件人的电话号码"/>

    <EditText 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:ems="10"

        android:inputType="phone"

        android:id="@+id/et_phone"/>

     <TextView 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="请输入信息的内容"/>

    <EditText 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:lines="5"

        android:inputType="textMultiLine"

        android:id="@+id/et_body"/>

    <Button 

        android:id="@+id/bt_sms"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello_world"/>

    

 

</LinearLayout>

打电话界面布局中:

<?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:background="#440000ff"

    android:orientation="vertical" >

    <EditText 

        android:id="@+id/et_ph"

        android:hint="请输入要拨打的电话号码"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:ems="10"

        android:inputType="phone"/>

    <Button 

        android:id="@+id/bt_call"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:text="拨打"/>

 

</LinearLayout>

存储空间布局中:

<?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:gravity="center"

    android:orientation="vertical" 

    android:background="#4400ff00">

 

    <TextView

        android:id="@+id/tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/cunchu" />

   

 

</LinearLayout>

代码逻辑如下:

MainActivity中:

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

Button start = (Button) findViewById(R.id.bt_start);

start.setOnClickListener(new OnClickListener() {public void onClick(View v) {

Intent intent = new Intent(MainActivity.this,homeActivity.class);

startActivity(intent);

}

});

}

第二个界面中:

public class homeActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_first);

}

public void open01(View  v){

//注意上下文为该类.即第一个参数

Intent intent = new Intent(homeActivity.this,smsActivity.class);

startActivity(intent);

}

public void open02(View v){

Intent intent = new Intent(homeActivity.this,phoneActivity.class);

startActivity(intent);

 

}

public void open03(View v){

Intent intent=new Intent(homeActivity.this,sdCardActivity.class);

startActivity(intent);

}

}

发短信界面中:

public class smsActivity extends Activity {

private EditText et_phone;

private EditText et_body;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

et_phone = (EditText) findViewById(R.id.et_phone);

et_body = (EditText) findViewById(R.id.et_body);

Button bt_sms = (Button) findViewById(R.id.bt_sms);

bt_sms.setOnClickListener(new OnClickListener() {

@SuppressWarnings("deprecation")

@Override

public void onClick(View v) {

String phone=et_phone.getText().toString().trim();

String body=et_body.getText().toString().trim();

if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(body)){

//注意在这里传参找对正确的界面

Toast.makeText(smsActivity.this, "电话号码或短信内容不能为空", 0).show();

}else{

//else可以省略,但是下面执行的语句只能是一句

SmsManager sms = SmsManager.getDefault();

sms.sendTextMessage(phone, null, body, null, null);

}

}

});

 

}

}

打电话界面中:

public class phoneActivity extends Activity {

private EditText et_ph;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_third);

et_ph = (EditText) findViewById(R.id.et_ph);

Button call=(Button) findViewById(R.id.bt_call);

call.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String phone=et_ph.getText().toString().trim();

if("".equals(phone)){

//注意传第一个参数的对象,容易出错

Toast.makeText(phoneActivity.this, "电话号码不能为空", 0).show();

}else{

Intent intent = new Intent();

intent.setAction(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel://"+phone));

startActivity(intent);

}

}

});

}

}

获取内存界面中:

public class sdCardActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_forth);

File dataFile = Environment.getDataDirectory();

File sdCard = Environment.getExternalStorageDirectory();

long dataSpace = dataFile.getTotalSpace();

long sdcardSpace = sdCard.getTotalSpace();

TextView tv=(TextView) findViewById(R.id.tv);

tv.setText("内部存储:"+Formatter.formatFileSize(this, dataSpace)+"\n"+"外部sd卡:"+Formatter.formatFileSize(this, sdcardSpace));

}

}

注意事项:

 * 一,不要忘记添加权限

 * 二,不要忘记在清单文件中注册Activity

 * 三,Toast时传第一个参数时要注意对象

 * 四,分清跳转页与初始页的顺序

 

 

 

这篇关于Android中多界面跳转的一个简单应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598283

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”