安卓APP开发基础

30 篇文章 6 订阅
订阅专栏

安卓工程构建第一个APP运行

嵌入式学习安卓目的其实是为了掌握简单的安卓APP开发,最重要的目的并不是为了开发APP,而是为了了解APP开发的整个过程,方便为安卓开发工程师提供SDK包,也就是如何在手机APP上实现点灯等控制硬件的一系列操作。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
点击Finish,程序构建完毕。

如何启动这个程序?由于自带的模拟器启动较慢,我们先去下载夜神模拟器(或者mumu模拟器,我是用的这个),运行。

eclipse和夜神建立连接之前,需要配置adb的环境变量(因为我们要在Windows的命令行终端使用这个指令):
找到之前我们安装eclipse的路径,找到adb.exe,如下图所示
在这里插入图片描述
复制这个路径,打开Windows环境变量配置界面(右击我的电脑,属性,高级系统设置,环境变量)

在这里插入图片描述

eclipse和mumu模拟器建立连接命令:

adb connect 127.0.0.1:7555

连接成功:
在这里插入图片描述

运行,工程名字上右键,run as android appclication,发现mumu模拟器出现运行界面。
在这里插入图片描述

在这里插入图片描述

  1. 构建容易,包名不能包含中文
  2. 模拟器一般用夜神和mumu,eclipse和夜神建立连接adb connect 127.0.0.1:62001 (127.0.0.1是网络本地地址,也就是连接自己,夜神模拟器的端口号是62001,mumu模拟器端口号是7555)
  3. 连接失败要重启adb adb kill-server adb start-server
  4. 运行,工程名字上右键,run as android appclication

安卓的工程目录文件夹介绍

在这里插入图片描述

  • src: 存放java代码的,有包,有文件

  • gen: 存放自动生成的文件,关心里面的R.java,保存程序的页面,按键,文本等控件的ID,它是自己生成的
    在这里插入图片描述

  • Android:4.4: 存放安卓开发的一些库(UI、蓝牙等),供开发者调用
    在这里插入图片描述

  • asserts: 存放一些资源,配置文件,图片

  • bin: 编译后会生成的一些文件,包括我们关心的apk

  • libs: 依赖库

  • res:
    *drawable前缀:存放app程序要用到的一些图片(高分辨率、低分辨率等)
    在这里插入图片描述
    *layout: 存放布局文件的文件夹
    一般一个activity(安卓页面)对应一个布局(可以拖拽和编程实现界面布局,复杂界面最好采用编程方式,避免出现界面变形,QT也存在这个问题。)
    在这里插入图片描述
    在这里插入图片描述
    *values: 存放一些参数,或者自定义控件的文件

  • AndroidMainfirst.xml: APP的配置
    *权限:网络访问权限,名片夹访问权限,相机访问权限
    *目标机器SDK版本
    *APP的名字
    *APP的图标
    *配置第一个被加载,启动页面

安卓APP启动过程

Laucher(不断检测屏幕触摸)->Androidmanifirst.xml->lauch标签的activity被加载->oncreat被调用->java关联xml布局页面->显示->等待用户触摸等操作

详细参照博文: https://www.jianshu.com/p/4d199e96ec60

安卓布局控件

布局的种类

详见 https://blog.csdn.net/wenzhi20102321/article/details/52677595
在这里插入图片描述

布局和页面的关系

显示一张美女图

将提前准备好的图片,选中复制,粘贴到下图目录下。添加一行代码,将图片成功添加到APP中。

在这里插入图片描述

activity_main.xml

<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"
	android:background="@drawable/bg"
    tools:context=".MainActivity" >

</RelativeLayout>

在这里插入图片描述

将别人的安卓工程源码导入到eclipse中的方法:
file->import->选中整个文件夹,如下图所示
这里是引用
打开后相应的代码:
在这里插入图片描述

显示两个美女

引入ID、below(指定ID位置下方)的使用方法。
布局关系:
在这里插入图片描述

activity_main.xml

<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" >
    
    
    <RelativeLayout 
        android:id="@+id/girl1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@drawable/girl1"
        ></RelativeLayout>
    
    <RelativeLayout 
        android:layout_below="@id/girl1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="@drawable/girl2"
        ></RelativeLayout>

</RelativeLayout>

在这里插入图片描述

常用布局之相对布局

RelativeLayout中子控件常用属性:

  • 1、相对于父控件,例如:android:layout_alignParentTop=“true”
    android:layout_alignParentTop 控件的顶部与父控件的顶部对齐;
    android:layout_alignParentBottom 控件的底部与父控件的底部对齐;
    android:layout_alignParentLeft 控件的左部与父控件的左部对齐;
    android:layout_alignParentRight 控件的右部与父控件的右部对齐;

  • 2、相对给定Id控件,例如:android:layout_above=“@id/**”
    android:layout_above 控件的底部置于给定ID的控件之上;
    android:layout_below 控件的底部置于给定ID的控件之下;
    android:layout_toLeftOf 控件的右边缘与给定ID的控件左边缘对齐;
    android:layout_toRightOf 控件的左边缘与给定ID的控件右边缘对齐;
    android:layout_alignBaseline 控件的baseline与给定ID的baseline对齐;
    android:layout_alignTop 控件的顶部边缘与给定ID的顶部边缘对齐;
    android:layout_alignBottom 控件的底部边缘与给定ID的底部边缘对齐;
    android:layout_alignLeft 控件的左边缘与给定ID的左边缘对齐;
    android:layout_alignRight 控件的右边缘与给定ID的右边缘对齐;

  • 3、居中,例如:android:layout_centerInParent=“true”
    android:layout_centerHorizontal 水平居中;
    android:layout_centerVertical 垂直居中;
    android:layout_centerInParent 父控件的中央;

activity_main.xml

<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" >
    
    
      <RelativeLayout 
        android:id="@+id/girl2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/testpic32"
        android:layout_centerInParent="true"
        ></RelativeLayout>
      
     

</RelativeLayout>

基础控件之Button,TextView,EditText,ImageView, padding和margin(内外边框)

由于拖拽的方式产生的控件容易跑飞,这里我们采用代码的方式去生成控件。
内外边框参照博文:https://www.cnblogs.com/dongh/p/9584962.html
在这里插入图片描述

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

 
    
    <RelativeLayout 
        android:layout_width="400dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:background="#ff0000"        
        >
        
       <ImageView 
       	android:id="@+id/image1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#ff0000"
        android:src="@drawable/user"
        android:layout_marginTop="30dp"
        />
        
        <TextView 
            android:layout_toRightOf="@id/image1"
            android:layout_alignBottom="@id/image1"
            android:layout_alignTop="@id/image1"
            android:layout_marginTop="10dp"
            android:id="@+id/user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:textSize="20dp"
            android:textColor="#ffffff"
            />
        
        <EditText 
            android:id="@+id/ed1"
            android:layout_width="320dp"
            android:layout_height="40dp"
            android:layout_toRightOf="@id/user"
            />
        
        <TextView 
            android:id="@+id/passwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            
            android:textSize="20dp"
            android:layout_below="@id/user"
            android:textColor="#ffffff"
            />
        
         <EditText 
            android:id="@+id/ed2"
            android:layout_width="320dp"
            android:layout_height="40dp"
            android:layout_toRightOf="@id/user"
            android:layout_below="@id/ed1"
            />
         
 
         
           <Button 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentRight="true"
             android:layout_marginRight="20dp"
             android:text="取消"
             android:id="@+id/btn2"
             android:layout_below="@id/ed2"
            
             />
        
           <Button 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginRight="30dp"
             android:text="确定"
             android:id="@+id/btn1"
             android:layout_below="@id/ed2"
             android:layout_toLeftOf="@id/btn2"
            
             />
        
    </RelativeLayout>
    
    
</RelativeLayout>

在这里插入图片描述
在这里插入图片描述

相对布局综合小演练-智能家居刷卡界面

在这里插入图片描述

<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"
	android:background="@drawable/bg_shopping_menu"
    tools:context=".MainActivity" >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="智能家居"
            android:textSize="25dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="6dp"
            />
        
        <Button 
            android:id="@+id/zhuce"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询信息"
            android:layout_toLeftOf="@id/zhuce"
            />
        
        
    </RelativeLayout>
    
    
    
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pic_rf"
        android:layout_centerInParent="true"
        />
    
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/card"
        android:layout_centerInParent="true"
        android:paddingLeft="120dp"
        />
   
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector"
        android:text="刷卡"
        android:layout_marginBottom="30dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

运行效果:
在这里插入图片描述

按键美化

方法:在drawable底下新建.xml文件(代码如下面博文中介绍),然后在主xml里面去引用这些美化的xml。
https://blog.csdn.net/tracydragonlxy/article/details/88552262
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

常用布局之线性布局

介绍

之前介绍的相对布局,自由拖拽控件时,摆放的位置是任意的,而线性布局在拖拽时,控件是不能任意放置的。只能在横线(默认)、竖线方向排列控件。

在这里插入图片描述

线性布局orientation、weight、gravity、divider属性

activity_main.xml

<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"
    android:background="@drawable/bg_shopping_menu"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:divider="@drawable/fenge"
            android:dividerPadding="2dp"
            android:orientation="vertical"
            android:showDividers="middle|end" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="账号" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="密码" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="ID号" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="5"
            android:divider="@drawable/fenge"
            android:dividerPadding="2dp"
            android:orientation="vertical"
            android:showDividers="middle|end" >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

在这里插入图片描述
fenge.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"
    >
    
    <size 
        android:width="200dp"
        android:height="2dp"
        />
    
    <stroke android:color="#000000"/>

</shape>

在这里插入图片描述

安卓按键响应的几种方式

在xml中设置按键的onClick绑定的函数

在这里插入图片描述
在这里插入图片描述
activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="buttonBeCliecked"
        android:text="按键一" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="53dp"
        android:onClick="buttonBeCliecked"
        android:text="按键二" />

</RelativeLayout>

MainActivity.java

package com.example.sgkbc.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void buttonBeCliecked(View v){
    	
    	//System.out.println(v.getId());
    	
    	switch(v.getId()){
	    	case R.id.button1:
	    		System.out.println("按键一被按下");
	    		Toast.makeText(this, "按键一被按下", 0).show();
	    		break;
	    		
	    	case R.id.button2:
	    		System.out.println("按键二被按下");
	    		Toast.makeText(this, "按键二被按下", 0).show();
	    		break;
    	
    	}

    }
  
    
}

自定义类实现按键监听事件的接口

参照博文: 安卓开发中的监听器(OnClickListener)

步骤一:绑定局部变量button和xml中的button, 使用API函数:findViewById
在这里插入图片描述
步骤二:实现接口类,在类中实现onclicked方法,写业务代码
在这里插入图片描述
步骤三:为1,2两步做的准备进行绑定,使得按键被按下后执行你写的业务代码
在这里插入图片描述

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  
        android:text="按键一" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="58dp"
        android:text="按键二" />

</RelativeLayout>

MainActivity.java

package com.example.sgkbc.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


//实现接口类,在类中实现onclicked方法,写业务代码
class MyClieckHandler implements View.OnClickListener{

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
    	case R.id.button1:
    		System.out.println("按键一被按下");
    	//	Toast.makeText(this, "按键一被按下", 0).show();
    		break;
    		
    	case R.id.button2:
    		System.out.println("按键二被按下");
    	//	Toast.makeText(this, "按键二被按下", 0).show();
    		break;
	
		}
	}
}

public class MainActivity extends Activity {

	Button btn1;
	Button btn2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        //绑定 局部变量button和xml中的button,  findViewById
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        
        //绑定 按键被按下后执行你写的业务代码
        btn1.setOnClickListener(new MyClieckHandler());
        btn2.setOnClickListener(new MyClieckHandler());
    }
    
}

匿名内部类实现按键响应

JAVA知识,匿名内部类由于没有名字,导致每次用它的时候都要建这个类,并实现里面的接口。

在这里插入图片描述

MainActivity.java


package com.example.sgkbc.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button btn1;
	Button btn2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        
        btn1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("按键一被按下");
		    	Toast.makeText(MainActivity.this, "按键一被按下", 0).show();
			}
		});
        
        btn2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("按键二被按下");
		    	Toast.makeText(MainActivity.this, "按键二被按下", 0).show();
			}
		});
    }
    
}

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  
        android:text="按键一" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="57dp"
        android:text="按键二" />

</RelativeLayout>

Activity实现click接口

在这里插入图片描述

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  
        android:text="按键一" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="58dp"
        android:text="按键二" />

</RelativeLayout>

MainActivity.java


package com.example.sgkbc.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
					      //继承			   //重写接口               
public class MainActivity extends Activity implements View.OnClickListener{

	Button btn1;
	Button btn2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
      
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
    	case R.id.button1:
    		System.out.println("按键一被按下");
    	//	Toast.makeText(this, "按键一被按下", 0).show();
    		break;
    		
    	case R.id.button2:
    		System.out.println("按键二被按下");
    	//	Toast.makeText(this, "按键二被按下", 0).show();
    		break;
	
		}
	}
    
}

安卓页面跳转

为安卓添加多个页面

在src下new一个.Activity文件,就新建了一个页面,继续new则继续添加页面。添加成功后会产生相对的JAVA以及xml文件,另外图中介绍了将相应页面设置成安卓启动界面的方法。

在这里插入图片描述

页面跳转方法

跳转的API使用方法:
在这里插入图片描述

介绍三个界面的跳转方法例子(一跳到二,二跳到三):

MainActivity.java

package com.example.sgkbc.activity;//注意快捷键自动导包的方法

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       
    }

    public void goSecond(View v){
    	 //intent去设置要跳转的页面
        Intent intent = new Intent(this, SecondActivity.class);
        //跳转
        startActivity(intent);
    	
    }
}

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goSecond"
        android:text="跳转到第二个页面" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="55dp"
        android:text="这是第一个页面" />

</RelativeLayout>

SecondActivity.java

package com.example.sgkbc.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
	}



    public void goThird(View v){
    	 //intent去设置要跳转的页面
        Intent intent = new Intent(this, ThirdActivity.class);
        //跳转
        startActivity(intent);
    	
    }

}

activity_second.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="113dp"
        android:text="这是第二个页面" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:onClick="goThird"
        android:text="跳转到第三个页面" />

</RelativeLayout>

ThirdActivity.java

package com.example.sgkbc.activity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ThirdActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.third, menu);
		return true;
	}

}

activity_third.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ThirdActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第三个页面" />

</RelativeLayout>

在这里插入图片描述
在这里插入图片描述

页面跳转传参

方法一:使用intent
传递:
在这里插入图片描述
接收:
在这里插入图片描述
MainActivity.java

package com.example.sgkbc.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       
    }

    public void goSecond(View v){
    	 //intent去设置要跳转的页面                   给第二个界面传递
        Intent intent = new Intent(this, SecondActivity.class);
        //方法的重载 可以放小数、整型数、数组等
        intent.putExtra("MyData", "爱你哦");//键 值
        //跳转
        startActivity(intent);
    	
    }
}

SecondActivity.java

package com.example.sgkbc.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends Activity {

	private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		//接收来自第一个界面
		Intent i = this.getIntent();//不能new一个 要在这个底下获取到这个Intent
		data = i.getStringExtra("MyData");// 键值
		//System.out.println("收到数据:"+data);
		Toast.makeText(this, "第二个页面收到数据:"+data, 0).show();
	}



    public void goThird(View v){
    	 //intent去设置要跳转的页面                  继续给第三个界面传
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("MyData", data);
        //跳转
        startActivity(intent);
    	
    }

}

在这里插入图片描述
方法二:使用bundle
发送:
在这里插入图片描述
接收:
在这里插入图片描述

MainActivity.java

package com.example.sgkbc.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       
    }

    public void goSecond(View v){
    	 //intent去设置要跳转的页面
        Intent intent = new Intent(this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("MyData", "爱你哦");
        bundle.putInt("ID", 521);
        intent.putExtras(bundle);
       // intent.putExtra("MyData", "爱你哦");
        //跳转
        startActivity(intent);
    	
    }
}

SecondActivity.java

package com.example.sgkbc.activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends Activity {

	//private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		Intent i = this.getIntent();
		//data = i.getStringExtra("MyData");//MyData
		//System.out.println("收到数据:"+data);
		Bundle b = i.getExtras();
		String data = b.getString("MyData");
		int data2 = b.getInt("ID");
		Toast.makeText(this, "第二个页面收到数据:"+data+data2, 0).show();
	}



    public void goThird(View v){
    	 //intent去设置要跳转的页面
        Intent intent = new Intent(this, ThirdActivity.class);
       // intent.putExtra("MyData", data);
        //跳转
        startActivity(intent);
    	
    }

}

在这里插入图片描述

安卓线程实现页面若干秒后自动跳转

实现页面的若干秒后的自动跳转效果
在这里插入图片描述

package com.example.sgkbc.activity;

import java.util.Scanner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        

        
        // 新线程里面去sleep.要另外启动一个线程,在3秒后启动一个新页面。
        //不要直接在UI线程中sleep(即直接Thread.sleep(3000)),
        //因为安卓启动后开启ui线程(不断刷新界面) 直接被sleep会暂停刷新,第一个界面就看不到了,三秒后跳到第二个界面
        
        //	如何在安卓(java)中创建线程
        Thread t = new Thread(new Runnable() {//设置线程要干的活
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				 try {//安卓线程固定使用方法 
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
				  }
				  
			        //intent去设置要跳转的页面
			        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
			        //跳转
			        startActivity(intent);
			}
		});
        
        t.start();//启动线程

    }

    public void goSecond(View v){
    	 //intent去设置要跳转的页面
        Intent intent = new Intent(this, SecondActivity.class);
        //跳转
        startActivity(intent);
    	
    }
}

运行安卓程序,发现三秒后自动跳转到第二个界面。

Activity(页面)的生命周期

参照博文——基础总结篇之一:Activity生命周期

在这里插入图片描述

1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态。

2.当前Activity被其他Activity覆盖其上或被锁屏:系统会调用onPause方法,暂停当前Activity的执行。

3.当前Activity由被覆盖状态回到前台或解锁屏:系统会调用onResume方法,再次进入运行状态。

4.当前Activity转到新的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法,进入停滞状态。

5.用户后退回到此Activity:系统会先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,再次进入运行状态。

6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足,杀死当前Activity,而后用户退回当前Activity:再次调用onCreate方法、onStart方法、onResume方法,进入运行状态。

7.用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法,结束当前Activity。

详细验证过程参照自己的代码或者上面博文的示例。

Android App 研发知识点以及开发工具方面知识
半夏微凉科技
04-30 1339
Android App 研发知识点以及开发工具方面知识: 1.Android Studio如何配置,如何导入项目、设置中文、运行项目以及生成apk文件; 2.Android UI基本操作、渲染性能优化、程序代码注释、动态设置信息、热更新、Handler消息处理、高并发、WebView相关优化以及Android相关截取信息等等操作。
AndroidAPP开发入门教程.pdf
07-13
AndroidAPP开发入门教程.pdf
10天学安卓-第八天
weixin_30914981的博客
01-25 186
昨天郑州雨夹雪,还有冰雹,结果小区就断电了,真是悲剧。第八天的学习就挪到今天了。 经过前几天的学习,我们了解了一些Android的基础知识,并且做出了一个也算实用的天气预报APP,对Android也算得上是入门了,那么今天我们继续改进我们的APP。 这个APP现在只能查看所在城市的天气,那么万一妹子不跟我们一个城市,我们就不能关注到妹子所在城市的天气了,那还怎么嘘寒问暖呢,这个问题是一定要解决...
Android使用开源框架完成城市列表三级联动(从服务端获取数据源和自定义json数据源)
wangxueqing52的博客
06-21 4267
Android-PickerView使用步骤:1.添加Jcenter仓库 Gradle依赖:compile 'com.contrarywind:Android-PickerView:4.1.4'2.在Activity中添加如下代码:package com.xueqing.reclerview.activity; import android.graphics.Color; import andr...
Android中开源免费天气预报接口以及全国地区代码(市级)
lpCrazyBoy的博客
11-15 3502
国家气象局提供的天气预报接口: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.html http://m.weather.com.cn/data/101010100.html 其中,第三个比较详细。 另外,也可以用下面的接口: Str...
实验三 综合示例设计与开发-“天气预报服务”软件
weixin_43803542的博客
06-20 3551
实验三 综合示例设计与开发-“天气预报服务”软件 一、实验目的 (1)理解Android 软件开发从准备策划工作开始到软件发布全过程; (2)掌握使用多种组件进行Android程序开发的方法。 二、实验环境 (1) 硬件:PC 机,其它相关硬件 ; (2)软件:Windows XP,Android Studio集成开发环境,Android Development Tools插件。 三、实验内容及 要求 (1)实现“天气预报服务”软件的开发与测试。 四、实验步骤 解决问题的思路:  实时天气数据从哪来? 
[Android]中国大部分城市地区的结构定义与按拼音排序
Sodino的专栏
09-01 1万+
项目中涉及到送货地址,录入工作量挺耗时的,分享出来,减免大家的重复劳动。 先见效果图如下:            本示例中使用Hashtable记录了中国大部分城市与地区的数据。其结构如下: Hashtable(Head)     ↑
Delphi XE8 iOS与Android移动应用开发(APP开发)[完整中文版]
07-26
Delphi XE8 iOS与Android移动应用开发(APP开发)[完整中文版],这是一本不可多得的介绍使用delphi xe8开发iOS与Android移动应用的电子书,实用的技术开发教程与技术开发手册。 Delphi XE8跨平台开发让你使用一套...
【android开发】Android APP开发入门教程Android APP开发入门教程
03-20
【android开发】Android APP开发入门教程Android APP开发入门教程 说明:Android APP开发入门教程Android APP开发入门教程 (Android APP Development tutorialAndroid APP Development tutorial) 文件列表: Android ...
Android APP开发入门教程
最新发布
03-27
Android APP开发入门教程 说明:Android APP开发入门教程Android APP开发入门教程 (Android APP Development tutorialAndroid APP ...android开发基础知识 - 副本.rar (479933, -06-14) Android APP开发入门教程
一看就懂的Android APP开发入门教程
01-21
于是凌晨一点睡不着写了第一个android程序HelloAndroid,po出来分享给其他也想学习android开发的朋友,这么傻瓜的Android开发入门文章,有一点开发基础的应该都能看懂。 一、准备工作 主要以我自己的开发环境为例,...
android 国际区号注册手机号编码 以及常用城市列表
再见孙悟空的专栏
10-20 1万+
附上 国际区号编码:我是定义到arrays.xml里面了 中国大陆+86 香港+852 澳门+853 台湾+886 阿联酋+971 阿根廷+54 奥地利+43 澳大利亚+61 爱沙尼亚+372
android中关于省市县地址大全
wust小吴
10-29 2459
不限地区 北京 天津 河北 山西 内蒙古 辽宁 吉林 黑龙江 上海 江苏 浙江 安徽 福建 江西 山东 河南 湖北
如何开发安卓_安卓app可视化开发 安卓快速app开发
weixin_33516557的博客
01-06 1496
自己咋开发APP这得看你的学习程度了,如果你学了安卓开发那么久按照教程来吧,如果没有学过,那么就看看我的回答是不是贴题意的。比较快的开发app方式。接入任意后台,通过HBuilder封装成app。2.使用MUI,借用官方的组件代码,构建app的界面,调用后台数据,再通过HBuilder封装,这个学的有点多。4.通过应用公园,中文可视化编程工具,编写app,因为是中文,比用其它语言明显容易一些。安卓...
安卓 Android之开发简单小应用(一)
热门推荐
Genven_Liang的博客
06-23 8万+
安卓 Android之开发简单小应用(一) 一、简述     记 --没学过Android之开发简单小应用。(课程设计作业)     例子打包:链接:https://pan.baidu.com/s/1LEQ1oWkUX8OmtfCFVydxWQ 密码:9o0d 二、环境搭建 软件打包:链接:https://pan.baidu.com/s/1VVsZqPrwOtvMuzeeJE1y_A 密...
win10基于QT开发手机安卓App
qq_43373204的博客
05-26 7448
QT安装 下载安装:QT5.14.2 安装包大小:2.3GB 清华园下载链接 、官网下载链接(需要FQ) 百度网盘链接:https://pan.baidu.com/s/12BSqht285yJrhiys8b4thw 提取码:xjmi 安装时仅选择如下几项安装,约占2.6GB空间 Qt5.14.2 √ MinGW 7.3.0 64-bit √ Android(用于开发Android手机APP) Developer and Designer Tools √ Qt Creator 4.11.1
android开发工具类(草鸡好用)
Losileeya
01-31 1万+
前言作为一个程序员有时候也会感觉很无奈,对于程序员,外行人总有着数不完的讽刺和误解,但是我都懒得去解释,代码搬运工人也好,民工也罢,随他们去说吧。 现在流行app程序员的泡沫化,说到这个搞安卓的是不是还是有相当的压力,但是呢还是不能阻挡我们学习的脚步,天朝的程序员也不怕别人说我们是代码搬运工,鲁迅都说拿来主义,我们山寨别人的效果怒抄别人的代码时我们不也一样在思考在加入自己的思维吗,对于有些我们不
Android天气预报丨极简版
midnight_time的博客
12-26 8037
文章主要介绍了使用Android开发一个极简版的天气预报的思路与流程。
第三章 arduboy游戏编程之helloworld
飘雪冰峰的博客
09-04 677
第三章 arduboy游戏编程之helloworld 这一章节比较简单,就是熟悉下 arduboy 的编程模板和仿真器的使用。 编程模板 #include <Arduboy2.h> Arduboy2 arduboy; void setup() { arduboy.begin(); arduboy.clear(); } void loop() { arduboy.clear(); arduboy.display(); } helloworld 代码 #include &l
安卓app开发 学习路线
04-29
安卓App开发学习路线如下: 1. Java语言基础:作为安卓开发的主要语言,你需要掌握Java语言的基础知识和面向对象编程的概念。 2. 安卓开发环境:下载安装安卓开发环境(Android Studio)并熟悉其使用。 3. 安卓...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
写文章

热门文章

  • Chrome如何下载网页视频 97565
  • GPIO输入输出模式原理(八种工作方式附电路图详解) 73395
  • Git的安装(附安装包) 39024
  • shell 脚本编写使用 32073
  • MATLAB 快速绘制曲线图的形状,粗细,颜色 31733

分类专栏

  • 全国计算机二级C语言考试操作题100套 付费 100篇
  • Tips 3篇
  • 电子信息大学生毕业课程设计 1篇
  • 电子电路 8篇
  • 正点 QT开发 3篇
  • IOT 38篇
  • 单片机 12篇
  • C语言基础 30篇
  • 全志H616 1篇
  • 嵌入式知识 53篇
  • Linux系统编程 14篇
  • 语音模块与Source Insight 2篇
  • 树莓派(ARM架构)开发 29篇
  • 智能家居 18篇
  • Java 9篇
  • 简单的安卓APP 30篇
  • 正点Ubuntu入门学习 23篇
  • 正点IMX6ULL裸机开发 35篇
  • 正点IMX6ULL系统移植 13篇
  • 正点IMX6ULL驱动开发 31篇
  • 正点 Linux C 应用编程 21篇
  • 迅为QT学习篇 2篇
  • STM32 44篇
  • 数据结构与算法 12篇
  • GitHub 12篇
  • sqlite 2篇
  • Tools 7篇

最新评论

  • 香橙派OrangePi AIpro,助力国产AIoT迈向新的台阶!

    CSDN-Ada助手: 恭喜你这篇博客进入【CSDN每天值得看】榜单,全部的排名请看 https://bbs.csdn.net/topics/618777589。

  • 硬件 TCP/IP 协议栈(SPI发送命令字)

    2301_79681476: 请问完整的代码包在正点原子哪里可以下载呀?

  • 江科大STM32 下

    周五: mpu6050当AD0=0时,从机地址为1101000。当AD0=1时,从机地址为1101001。文中好像有误表情包

  • STM32F103五分钟入门系列(二)GPIO的七大寄存器+GPIOx_LCKR作用和配置

    weixin_48879489: 问题解决了吗?我的LCKR锁定也是一样的问题,始终是0没变化

  • 江科大STM32 上

    m0_68705692: 友友,可以给一下pdf版本嘛,万分感谢

大家在看

  • 社群商学院——用社群做天下生意
  • 【高能物理.ROOT】一个Bin的信号强度上限的假设检验(下)
  • c++类与对象
  • C51学习归纳5 --- 定时器 528
  • git版本控制工具常用命令 373

最新文章

  • 香橙派OrangePi AIpro,助力国产AIoT迈向新的台阶!
  • PPP协议与AT指令驱动4G模组
  • 2024届毕设 基于STM32的农业大棚环境监控系统设计与实现(源码+硬件+论文+答辩)
2024年7篇
2023年65篇
2022年61篇
2021年423篇

目录

目录

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行稳方能走远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或 充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值

PHP网站源码珠海营销网站多少钱朝阳关键词排名烟台网站优化按天收费报价襄阳百姓网标王推广推荐铜川百度网站优化排名哪家好北海网站推广工具公司江门外贸网站设计价格徐州网站推广方案多少钱醴陵网站优化按天计费价格坂田网站关键词优化哪家好丽江网站推广系统报价山南关键词按天收费多少钱徐州推广网站中山网站优化按天收费推荐绵阳网站定制宜昌百度爱采购推荐沈阳SEO按天收费坂田百度网站优化排名多少钱广元设计公司网站报价蚌埠百姓网标王推广哪家好大运网站建设价格拉萨SEO按效果付费哪家好景德镇阿里店铺运营天门seo网站推广报价新乡百姓网标王推广推荐大理seo网站推广公司吉安设计公司网站多少钱鹤壁网站搜索优化公司张家界网站改版多少钱酒泉关键词按天收费多少钱歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

PHP网站源码 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化