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

相关文章

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知