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

相关文章

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

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. 基础布局