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

相关文章

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Python Flask 库及应用场景

《PythonFlask库及应用场景》Flask是Python生态中​轻量级且高度灵活的Web开发框架,基于WerkzeugWSGI工具库和Jinja2模板引擎构建,下面给大家介绍PythonFl... 目录一、Flask 库简介二、核心组件与架构三、常用函数与核心操作 ​1. 基础应用搭建​2. 路由与参

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

电脑系统Hosts文件原理和应用分享

《电脑系统Hosts文件原理和应用分享》Hosts是一个没有扩展名的系统文件,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应... Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

Java应用如何防止恶意文件上传

《Java应用如何防止恶意文件上传》恶意文件上传可能导致服务器被入侵,数据泄露甚至服务瘫痪,因此我们必须采取全面且有效的防范措施来保护Java应用的安全,下面我们就来看看具体的实现方法吧... 目录恶意文件上传的潜在风险常见的恶意文件上传手段防范恶意文件上传的关键策略严格验证文件类型检查文件内容控制文件存储