Android 基础知识之Activity

来源:互联网 发布:linux下如何卸载jdk 编辑:程序博客网 时间:2024/06/04 11:18


本人处于学习Android的初级阶段,写博客主要用于自己对于学习知识的总结,有不当之处望众位大神勿喷,欢迎大家批评斧正!!

Activity为Android的四大组件之一,其特点大致为:

       1.它的类必须实现特定的接口或继承特定的类;2.需要在配置文件中(AndroidManifest.xml功能清单文件)配置全类名(即注册);3.它的对象不是通过New创建的而是系统创建的;4.它的对象有一定的生命周期,它的类中有对应的生命周期回调方法。以上4个特点是Android四大组件共有的特点!

Activity的跳转:

       1.显式跳转;特点:知道目标Activity的类名或全类名的字符串形式;

                            步骤:1.创建Intent对象;2.指定起始地和目的地;3.启动Activity;4.关闭Activity。

       2.隐式跳转;特点:不知道目标Activity的类名或全类名的字符串形式,只指定其他的一些信息,如action

                            步骤:1.创建Intent对象;2.指定action、category或data等;3.启动Activity;4.关闭Activity。

                            (实例如:跳转到系统的应用就像打电话或者发短信)

       二者的区别:一般来讲跳转到当前应用用显式意图,而跳转到其他应用则用隐式意图;

Activity的传值:

正向传值:(如从A跳转到B)

发送方:1.创建Intent对象,指定起始地和目的地;2.设置携带数据;(intent.putExtra(键,值))3.启动Activity。

接收方:1.获取Intent对象;2.获取携带数据(intent.getXXXExtra(键));

回调传值:(带返回值的传值)

发送方:1.创建Intent对象,指定起始地和目的地;2.启动Activity.StartActivityForResult();3.重写回调方法OnActivityResult(),其中携带含有参数的intent对象。

接收方:1.获取Intent对象,getIntent()方法;2.设置携带数据intent.putExtra(键 值);3.设置结果setResult();4.关闭当前界面finish();

Activity的生命周期:(android四大组件的共有特点)

Activity的生命周期共有七个方法:

onCreate();onStart();onResume();onPause();onStop();onDestroy();onRestart();

生命周期的四种状态:活动(可见也可对其进行操作);暂停(可见但不可操作);停止(不可见对对象依然存在);销毁也称为死亡(不可见也不存在对象)。

举个例子加深理解:

1.从A页面的按钮点击跳转到B页面,其中B页面完全覆盖A,这个过程中具体生命周期的先后顺序为:

A-->onPause()-->B-onCreate()-->onStart()-->onResume()-->A-onStop();

2.当点击返回按钮的时候有发生了什么呢?

B-->onPause()-->A-onRestart()-->onStart()-->onResume()-->B->onStop()-->onDestroy();

3.从A页面的按钮点击跳转到B页面,其中B页面完全覆盖A以对话框的方式弹出,这个过程中具体生命周期的先后顺序为:

A-->onPause()-->B-onCreate()-->onStart()-->onResume();

4.当点击返回或取消按钮的时候:

B-->onPause()-->A-onResume()-->B->onStop()-->onDestroy();

Activity的运行模式:

1.传统模式:栈管理结构,先进后出;

2.设置运行模式,在Activity配置(注册)的时候添加属性LunchMode;

3.四种运行模式

standard:
标准模式,每次调用startActivity()方法就会产生一个新的实例。这是默认的模式
singleTop:
如果已经有一个实例位于Activity栈的顶部时,就不产生新的实例;如果不位于栈顶,会产生一个新的实例。没有网络的情况用户拼命点击,如果把当前Activity设置singleTop,就不会卡。
singleTask:
只有一个实例,默认在当前Task中,会把它上面的activity杀死掉。应用在注册的情境。
singleInstance:
只有一个实例, 创建时会新建一个栈, 且此栈中不能有其它对象;例如浏览器,单独去访问网页的时候。

补充一点:事件处理

基于监听器:

组件.setOnXXXLinstener(匿名内部类)

组件.setOnXXXLinstener(this)

组件.setOnXXXLinstener(自定义接口的实现类对象)

事件:点击、长按等动作;

事件源:点击的是谁,如点击某个按钮,则那个按钮就是事件源;

监听器:检测用户的动作,android提供的接口类。

最后做个小小的案例:登陆、注册的跳转;

MainActivity代码:

import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;public class MainActivity extends AppCompatActivity {    private EditText editText1;    private EditText editText2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //实例化两个文本编辑器        editText1 = (EditText) findViewById(R.id.editText1);        editText2 = (EditText) findViewById(R.id.editText2);    }    public void register(View v) {        //1.得到数据        String name = editText1.getText().toString().trim();        String passWord = editText1.getText().toString().trim();        //2.创建intent对象        Intent intent = new Intent(MainActivity.this, RegisterActivity.class);        //3.把内容携带到RegisterActivity里面        intent.putExtra("name", name);        intent.putExtra("passWord", passWord);        //请求码        int requestCode = 1;        //带回调的启动        startActivityForResult(intent, requestCode);    }    //重写onActivityResult方法    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        //判断条件        if (requestCode == 1 && resultCode == RESULT_OK) {            //得到携带过来的数据            String resultName = data.getStringExtra("resultName");            String resultPassWord = data.getStringExtra("resultPassWord");            //显示到MainActivity上的EditText上            editText1.setText(resultName);            editText2.setText(resultPassWord);        }    }}
MainActivity的布局文件:
<?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">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="账号:"            android:textColor="#000000"            android:textSize="20sp" />        <EditText            android:id="@+id/editText1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入账号"            android:textSize="20sp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密码:"            android:textColor="#000000"            android:textSize="20sp" />        <EditText            android:id="@+id/editText2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入密码"            android:textSize="20sp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="登陆"            android:textColor="#000000"            android:textSize="20sp" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="register"            android:text="注册"            android:textColor="#000000"            android:textSize="20sp" />    </LinearLayout></LinearLayout>

注册界面的代码实现:

import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;public class RegisterActivity extends AppCompatActivity {    private EditText editText3;    private EditText editText4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_register);        //实例化控件        editText3 = (EditText) findViewById(R.id.editText3);        editText4 = (EditText) findViewById(R.id.editText4);        //得到从MainActivity页面传入的数据        String name = getIntent().getStringExtra("name");        String passWord = getIntent().getStringExtra("passWord");        //把传入的数据设置到EditText上        editText3.setText(name);        editText4.setText(passWord);    }    public void back(View v) {        //得到编辑框的数据        String resultName = editText3.getText().toString().trim();        String resultPassWord = editText4.getText().toString().trim();        //得到意图        Intent intent = getIntent();        //携带结果数据        intent.putExtra("resultName",resultName);        intent.putExtra("resultPassWord",resultPassWord);        //返回数据        setResult(RESULT_OK,intent);        //关闭当前页面        finish();    }}


注册界面的布局文件:

<?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">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="账号:"            android:textColor="#000000"            android:textSize="20sp" />        <EditText            android:id="@+id/editText3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入账号"            android:textSize="20sp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密码:"            android:textColor="#000000"            android:textSize="20sp" />        <EditText            android:id="@+id/editText4"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入密码"            android:textSize="20sp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="注册"            android:textColor="#000000"            android:textSize="20sp" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="back"            android:text="返回"            android:textColor="#000000"            android:textSize="20sp" />    </LinearLayout></LinearLayout>



这是住主页面图片:

这是register页面的图片;

这个案例实现了带返回数据的跳转一般用于登陆注册!!!!



1 0