Android stdio笔记

Android stdio笔记,第1张

概述下载Androidstdio,直接在官网下载。新建一个project。按钮在activity_main.xml文件中输入<Buttonandroid:id="@+id/button1"//新增一个名为“button”的资源idandroid:text="Button1"//按钮上的文本android:layout_width="match_parent"//la

下载AndroID stdio,直接在官网下载。
新建一个project。

按钮

在activity_main.xml文件中输入

<button        androID:ID="@+ID/button1"//新增一个名为“button”的资源ID        androID:text="button1"//按钮上的文本        androID:layout_wIDth="match_parent"//layout_wIDth就是父布局允许vIEw所占的宽度        androID:layout_height="wrap_content"//高度        tools:ignore="MissingConstraints" />

在</androIDx.constraintlayout.Widget.ConstraintLayout>前面

match_parent表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小

wrap_content表示让当前的控件大小能够刚好包含里面的内容,也就是由控件内容决定当前控件的大小

此时会发现按钮上的文本“botton1”是全大写字母“BottON1”,以下的修改方法:
找到AndroIDManifest.xml找到

androID:theme="@style/Apptheme">

按Ctrl键左键进入主题
在前面添加一个项目

<item name="androID:textAllCaps">false</item>//把文本全部大写这个功能关闭


然后回到“MainActivity.java”源文件中添加对应的对象button1

private button button1;


这里调用了setContentVIEw()方法来给当前的活动加载一个布局,此时创建的activity_main.xml布局的资源ID会添加到R文件中,只需要调用R.layout.activity_main就可以得到activity_main.xml布局的ID,然后将这个值传入setContentVIEw()方法即可。

此时button类所在的包将会自动导入进来,如图


接下来初始化视图

 	/**     * 初始化视图     */    public voID initVIEw() {        button1 = findVIEwByID(R.ID.button1);        }

此时打开模拟器会出现按钮,按钮可以点击但没有任何反应。

另外补充:
AndroIDmanifest.xml就是用来写App的必要的配置。可AndroIDmanifest.xml中的icon、label、theme等处在res文件夹中分别修改APP的图标、标签(名称)和主题等等。

toast(吐司,在AndroID中表示消息提示框)
 /**     * 初始化视图     */    public voID initVIEw() {        button1 = findVIEwByID(R.ID.button1);        //为按钮添加监听器        button1.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                //onClick方法会在button1这个按钮按下时自动被调用执行//           Toast.makeText(MainActivity.this, "Hello你好呀", Toast.LENGTH_LONG).show();                //显示Intent(意图)//                Intent intent = new Intent(MainActivity.this, SecondActivity.class);//                startActivity(intent);                //隐式Intent//                Intent intent = new Intent("com.androIDstudy.activitytest.START_ACTION");//                startActivity(intent);                /*                //通过隐式Intent调用系统应用                Intent intent = new Intent();                intent.setAction(Intent.ACTION_CALL);                // intent.setAction(Intent.ACTION_DIAL);                intent.setData(Uri.parse("tel:12345678910"));//拨号                //intent.setAction("AndroID.settings.SETTINGS");   //打开设置窗口                startActivity(intent);*/                Intent intent = new Intent(MainActivity.this, SecondActivity.class);                /*                intent.putExtra("Data","Hello SecondActivity");                startActivity(intent);     //传递数据                 */                startActivityForResult(intent,1);            }        });    }

打出Toast在AndroID stdio强大的功能支持下就会出现如图


其中MainActivity.this表示的意思context(上下文)
类名.this 这种语法在什么情况下会出现呢?

一句话:内部类需要用到外部类的成员变量或方法。

附加说明:如果重名则必须“类名.this”指定那个类的成员变量或方法,如果没有重名则该变量可以指定所属类也可以不指定所属类。

其中text表示显示文本

其中第三个表示显示时长

menu(菜单)

首先在res文件夹里新建一个名为“menu”的文件夹,一定要取为“menu”。在该文件夹中新建一个menu resource file。
在menu.xml文件中在前面添加这两行

 	<item androID:ID="@+ID/add_item" androID:title="Add"/>    <item androID:ID="@+ID/remove_item" androID:title="Remove"/>

每一行表示菜单的一项,包括ID和Title(标题)

在MainActivity中创建一个onCreateOptionsMenu()方法
OptionsMenu的意思是选项菜单,还有其他类型的菜单:上下文菜单

@OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu,menu);        return super.onCreateOptionsMenu(menu);    }

getMenuInflater().inflate(R.menu.menu,menu);这一行中通过getMenuInflater()方法能够得到MenuInflate对象,inflate()方法接收两个参数,第一个参数用于指定我们通过哪一个资源文件来创建菜单;第二个参数用于指定我们的菜单项将添加到哪一个Menu对象当中。

@OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch(item.getItemID()){            case R.ID.add_item:                Toast.makeText(this, "Add_Item clicked", Toast.LENGTH_SHORT).show();                break;            case R.ID.remove_item:                Toast.makeText(this, "Remove_Item clicked", Toast.LENGTH_SHORT).show();                break;        }        return super.onoptionsItemSelected(item);    }
显式Intent

(打开新的activity)

在mainactivity.java所在文件夹中新建一个activity

在activity_second.xml中添加一个按钮

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".SecondActivity">    <button        androID:ID="@+ID/button2"        androID:text="button2"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        tools:ignore="MissingConstraints" /></androIDx.constraintlayout.Widget.ConstraintLayout>


现在的需求是点击MainActivity的按钮能从MainActivity切换到secondActivity上。

//显式Intent(意图)Intent intent = new Intent(MainActivity.this,SecondActivity.class);startActivity(intent);
隐式intent
//隐式Intent//                Intent intent = new Intent("com.androIDstudy.activitytest.START_ACTION");//这么长的东西其实是自定义的,可以任意地改//                startActivity(intent);                         //通过隐式Intent调用系统应用                Intent intent = new Intent();                intent.setAction(Intent.ACTION_CALL);                // intent.setAction(Intent.ACTION_DIAL);//DIAL是转到电话输入了对应号码的界面但为拨号                intent.setData(Uri.parse("tel:12345678910"));//拨号                //intent.setAction("AndroID.settings.SETTINGS");   //打开设置窗口                //intent.setAction("com.androID.contacts.action.List_CONTACTS")//打开联系人窗口                startActivity(intent);

此时会发现APP打不开,因为在手机中该APP没有申请打电话的权限

要在AndroIDManifest.xml中写这一句

<uses-permission androID:name="androID.permission.CALL_PHONE"/>


然后在手机设置权限里面允许该APP打电话的权限

若要实现其他更多的功能,可以在androID api文档中查找
https://developer.android.google.cn/guide

向下一个activity传递数据
intent.putExtra("Data","Hello SecondActivity");                startActivity(intent);     //传递数据

其中“Data”表示传递数据的名称,“Hello SecondActivity”表示传递的数据


在SecondActivity.java文件中写

 Intent intent = getIntent();        String data = intent.getStringExtra("Data");        Log.d("SecondActivity", data);        initVIEw();

向上一个activity返回数据
startActivityForResult(intent,1);//第二个参数是requestCode

其中,requestCode表示:如果> = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity。


在SecondActivity中写

 /*    初始化窗口     */    public voID initVIEw(){        button2 = findVIEwByID(R.ID.button2);        button2.setonClickListener(new VIEw.OnClickListener(){            @OverrIDe            public voID onClick(VIEw vIEw){                Intent intent = new Intent();                intent.putExtra("data_return","Hello MainActivity");                setResult(RESulT_OK,intent);                finish();//关闭窗口            }


在MainActivity中写

@OverrIDe    protected voID onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {        switch (resultCode) {            case 1:                if (resultCode == RESulT_OK) {                    String resultData = data.getStringExtra("data_return");                    Log.d("MainActivity", resultData);                }                break;        }

活动的生命周期1.AndroID使用任务(Task)来管理活动

一个任务就是一组存放在栈里的活动的集合,这个栈被称为 返回栈(Back Stack) 。

栈是一种后进先出的数据结构。

当启动一个新活动,便会在返回栈中入栈,并处于栈顶的位置。

当按下Back键或者finish()方法销毁一个活动,处于栈顶位置的活动会出栈,前一个入栈的活动重新处于栈顶的位置。

系统总是显示处于栈顶的活动给用户。

2.活动状态

(1)运行状态:一个活动位于返回栈的栈顶,该活动便处于运行状态。

系统不愿意收回运行状态的活动,会导致用户体验很差。

(2)暂停状态:一个活动不再处于栈顶,但依然可见时,该活动便处于暂停状态。

系统也不愿意回收这类活动,因为它还是可见的,回收可见的东西会影响用户的体验。只有内存极低的情况下,系统会考虑回收这类活动。

(3)停止状态:一个活动不再处于栈顶位置,并且完全不可见时,该活动便处于停止状态。

系统会为这类活动保存相应的状态和成员变量,但是并不可靠,当其他地方需要内存时,该状态的活动很可能被系统回收。

(4)销毁状态:一个活动从返回栈中被移除,便处于销毁状态。

系统倾向于回收这类活动,保证手机的内存充足。

3.活动的生存期

Activity类定义了7个回调方法。

(1)onCreate():在活动第一次被创建的时候调用。在这个方法中完成活动的初始化 *** 作,例如加载布局、绑定事件等

(2)onStart():在活动由不可见变成可见的时候调用。

(3)onResume():在活动准备好和用户进行交互的时候调用。此时活动位于返回栈的栈顶,并且处于运行状态。

(4)onPause():在系统准备去启动或者恢复另一个活动的时候调用。在该方法中将一些销毁cpu的资源释放掉,保存关键数据。该方法执行速度一定要快,不然会影响栈顶活动的使用

(5)onStop():在方法完全不可见的时候调用。注意onStop()与onPause()的区别。

(6)onDestroy():在活动被销毁之前调用。

(7)onRestart():在活动由停止状态变为运行状态前调用。

以上七种方法中除了onRestart()方法,其他都是两两相对的,从而又可以将活动分为3种生存期

(1)完整生存期 活动在onCreate()方法和onDestroy()方法之间经历的,就是完整生存期。一般情况下,活动会在onCreate()方法中完成各种初始化 *** 作,而在onDestory()方法中完成释放内存的 *** 作

(2)可见生存期 活动在onStart()方法和onStop()方法之间经历的 就是可见生存期。在可见生存期内,活动对于用户总是可见的,即便可能无法与用户进行交互。

(3)前台生存期 活动在onResume()方法和onPasue()方法之间经历的就是前台生存期。在前台生存期内,活动总处于运行状态,此时活动是可以和用户交互的。

@H_502_319@

体验activity生命周期

新建一个APP,命名为ActivityRecycleTest,在MainActivity.java文件同目录下新建一个activity,命名为normalActivity

在activity_normal.xml中写

 <TextVIEw        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:textSize="16sp"        androID:text="This is a normal activity"        tools:ignore="MissingConstraints" />

那句tools:ignore="MissingConstraints"是系统给的,不知道是什么意思,书里是没有这句的。

然后再新建一个activity,命名为DialogActivity

在activity_main.xml中写

 <button        androID:ID="@+ID/btn_start_normal_activity"        androID:text="StartnormalActivity"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        tools:ignore="MissingConstraints" />    <button        androID:ID="@+ID/btn_start_dialog_activity"        androID:text="StartDialogActivity"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        tools:ignore="MissingConstraints" />

定义两个按钮,但很奇怪,布局只显示一个按钮,如图


解决方法:把布局改成linearLayout,方向orIEntation改成vertical(垂直)
如下:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:orIEntation="vertical"    androID:visibility="visible"    tools:context=".MainActivity"    tools:visibility="visible">    <button        androID:ID="@+ID/btn_start_normal_activity"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:text="StartnormalActivity"        tools:ignore="MissingConstraints" />    <button        androID:ID="@+ID/btn_start_dialog_activity"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:text="StartDialogActivity"        tools:ignore="MissingConstraints" /></linearLayout>

然后再MainActivity中声明这两个类

private button btnStartnormalActivity,btnStartDialogActivity;

在MainActivity中,这么写:

public class MainActivity extends AppCompatActivity {    private button btnStartnormalActivity,btnStartDialogActivity;    private static final String TAG = "MainActivity";    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        initVIEw();        Log.d(TAG,"onCreate()");    }    @OverrIDe    protected voID onStart() {        super.onStart();        Log.d(TAG,"onStart()");    }    @OverrIDe    protected voID onResume() {        super.onResume();        Log.d(TAG,"onResume()");    }    @OverrIDe    protected voID onPause() {        super.onPause();        Log.d(TAG,"onPause()");    }    @OverrIDe    protected voID onStop() {        super.onStop();        Log.d(TAG,"onStop()");    }    @OverrIDe    protected voID onDestroy() {        super.onDestroy();        Log.d(TAG,"onDestroy()");    }    @OverrIDe    protected voID onRestart() {        super.onRestart();        Log.d(TAG,"onRestart()");    }    /**     * 初始化窗口     */    public voID initVIEw(){        btnStartnormalActivity = findVIEwByID(R.ID.btn_start_normal_activity);        btnStartDialogActivity = findVIEwByID(R.ID.btn_start_dialog_activity);        btnStartnormalActivity.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = new Intent(MainActivity.this,normalActivity.class);                startActivity(intent);            }        });        btnStartDialogActivity.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = new Intent(MainActivity.this,DialogActivity.class);                startActivity(intent);            }        });    }}

接下来要把DialogActivity改成对话框的形式
在AndroIDmanifest.xml中将

 <activity androID:name=".DialogActivity"></activity>

改成

 <activity androID:name=".DialogActivity"            androID:theme="@style/theme.AppCompat.Dialog"></activity>

最后开始运行,不点击按钮或点击对应按钮就可以在logcat中看到生命周期过程

活动的启动模式

四种启动模式:

standard
singletop
singleTask
singleInstance
修改活动的启动模式,通过xml活动中的”androID:launchMode”修改,AndroID通过返回栈来管理活动。


新建一个project,命名为LaunchModeTest。
将MainActivity.java改名为FirstActivity

在Activitymanifest.xml中将

<activity androID:name=".FirstActivity">

更改为

<activity androID:name=".FirstActivity" androID:launchMode="standard">

在FirstActivity.xml中生成一个按钮

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".FirstActivity">    <button        androID:ID="@+ID/button1"        androID:text="button1"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        tools:ignore="MissingConstraints" /></androIDx.constraintlayout.Widget.ConstraintLayout>

FirstActivity.java中这么写

package com.example.launchmodetest;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;public class FirstActivity extends AppCompatActivity {    private button button1;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        Log.d("FirstActivity",this.toString());        initVIEw();    }    private voID initVIEw() {        button1 = findVIEwByID(R.ID.button1);        button1.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = new Intent(FirstActivity.this,FirstActivity.class);                startActivity(intent);            }        });    }}

运行后,点击按钮会发现跳出新的重复的界面,点击多次就多次跳出。

如果把在Activitymanifest中将模式改成singletop就不会重复跳出。

一键退出APP

总结

以上是内存溢出为你收集整理的Android stdio笔记全部内容,希望文章能够帮你解决Android stdio笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1049644.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存