Activity的跳转

 

一个Android 应用通常有多个界面,也就说有多个Activity,那么如何从一个Activity 跳转到另外一个Activity 呢?以及跳转的时候如何携带数据呢?

Activity 之间的跳转分为2 种:

 

显式跳转:在可以引用到另外一个Activity 的字节码,或者包名和类名的时候,通过字节码,或者包名+类名的方法实现的跳转叫做显示跳转。显示跳转多用于自己工程内部多个Activity 之间的跳转,因为在自己工程内部可以很方便地获取到另外一个Activity 的字节码。

 

隐式跳转:隐式跳转不需要引用到另外一个Activity 的字节码,或者包名+类名,只需要知道另外一个Activity AndroidManifest.xml 中配置的intent-filter 中的action category 即可。言外之意,如果你想让你的Activity 可以被隐式意图的形式启动起来,那么就必须为该Activity 配置intent-filter

Activity 之间的跳转都是通过Intent 进行的。Intent 即意图,不仅用于描述一个Activity的信息,同时也是一个数据的载体。

开启界面的两种方式

  • 显示意图的用法:
  1. 通过Intent 的带参构造函数

Intent intent = new Intent(this, SecondActivity.class);

  1. 通过Intent setClass 方法

intent.setClass(this, SecondActivity.class);

Intent 可以携带的数据类型

  1. 八种基本数据类型booleanbytecharshortintfloatdoublelong String 以及这9 种数据类型的数组形式
  2. 实现了Serializable 接口的对象
  3. 实现了Android Parcelable 接口的对象以及其数组对象

 

  • 隐式意图的用法
  1. 要跳转的activity在清单文件里增在intent-filter

<intent-filter >

        <action android:name="自己定义,习惯用包名后加功能名"/>

        <category android:name="android.intent.category.DEFAULT"/> //默认

</intent-filter>

  1. 谁要跳转到这个activity,谁的方法里面调用

Intent intent = new Intent();

intent.setAction("要跳转的activity在清单文件里配置的action");

intent.addCategory("android.intent.category.DEFAULT");-->默认

startActivity(intent);

  • 隐示意图需要注意的地方

在清单文件的 intent-filter 里面还可以配置 data标签,data标签可以配置多个不同种类型的

例如:

<data androidscheme="自己定义"/> -->设置前缀,与电话播放器调用很像

<data android:mimeType="text/plain"/> -->定义类型,这里不能随意定义

 

java代码里,如果同时配置了schememineType

intent.setDataAndType(scheme,mimeType);

 

如果只配置scheme:

intent.setData();

 

如果只配置了mimeType:

intent.setType();

 

案例-人品计算器

综合了activity的创建,显示意图打开界面,数据的传递的一个案例。

需求分析:

主要有三个界面,分别是首页MainActivity,计算页面CalcActivity,结果界面ResultActivity。实现的效果是首页显示一个logo界面停留两秒后进入到计算页面,在计算界面输入需要测试的姓名,并勾选对应的性别点击计算按钮即可进入结果界面查看计算的结果。

具体的效果及流程如下图:

  2S后自动进入

 

输入姓名后计算的结果界面:

 

 

代码实现

  1. 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:layout_centerInParent="true"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/logo" />

 

</RelativeLayout>

  1. MainActivity代码自动等待两秒进入CalcActivity

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//方式一

new Thread(){

public void run() {

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

Intent intent = new Intent();

intent.setClass(MainActivity.this, CalcActivity.class);

startActivity(intent);

finish();//mainActivity关闭

};

}.start();

//方式二:

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

Intent intent = new Intent();

intent.setClass(MainActivity.this, CalcActivity.class);

startActivity(intent);

finish();//mainActivity关闭

}

}, 2000);

}

}

  1. 计算界面布局XML

<?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" >

 

    <ImageView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:src="@drawable/logo" />

 

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="请选择用户的性别:" />

 

    <RadioGroup

        android:id="@+id/rg_sex"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <RadioButton

            android:id="@+id/rb_male"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:checked="true"

            android:text="" />

 

        <RadioButton

            android:id="@+id/rb_female"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="" />

 

        <RadioButton

            android:id="@+id/rb_unknow"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="未知" />

    </RadioGroup>

 

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <EditText

            android:id="@+id/et_name"

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="3"

            android:hint="请输入要计算的姓名" />

 

        <Button

            android:layout_width="0dip"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:onClick="calc"

            android:text="计算" />

    </LinearLayout>

 

</LinearLayout>

  1. 计算界面代码实现:获取输入的姓名和选中的性别,传递到ResultActivity界面

public class CalcActivity extends Activity {

private EditText et_name;

private RadioGroup rg_sex;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_calc);

et_name= (EditText) findViewById(R.id.et_name);

rg_sex = (RadioGroup) findViewById(R.id.rg_sex);

}

 

public void calc(View view){

String name = et_name.getText().toString().trim();

if(TextUtils.isEmpty(name)){

Toast.makeText(this, "姓名不能为空", 0).show();

return;

}

 

//把字符串的数据传递给第三个界面

Intent intent = new Intent(this, ResultActivity.class);

intent.putExtra("name", name);//在意图对象里面携带要传递的字符串数据

intent.putExtra("sex", rg_sex.getCheckedRadioButtonId());

intent.putExtra("bitmap", BitmapFactory.decodeResource(getResources(), R.drawable.logo));

startActivity(intent);

}

}

  1. ResultActivity界面布局xml

<?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="#44ff0000"

    android:gravity="center"

    android:orientation="vertical" >

 

    <ImageView

        android:id="@+id/iv"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

 

    <TextView

        android:id="@+id/tv_result"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="结果:"

        android:textColor="#000000"

        android:textSize="18sp" />

 

</LinearLayout>

  1. ResultActivity具体逻辑实现

public class ResultActivity extends Activity {

TextView tv_result;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_result);

tv_result = (TextView) findViewById(R.id.tv_result);

// 获取传递过来的Intent

Intent intent = getIntent();

// 取出对应的数据

String name = intent.getStringExtra("name");

Bitmap bitmap = intent.getParcelableExtra("bitmap");

ImageView iv = (ImageView) findViewById(R.id.iv);

iv.setImageBitmap(bitmap);

int rb_id = intent.getIntExtra("sex", R.id.rb_male);

 

byte[] result = null;

try {

switch (rb_id) {

case R.id.rb_male:// 男性

result = name.getBytes();

break;

 

case R.id.rb_female:// 女性

result = name.getBytes("gb2312");

break;

 

case R.id.rb_unknow:// 未知性别:

result = name.getBytes("iso-8859-1");

break;

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

int total = 0;

for (byte b : result) {

int number = b & 0xff; // -128~127

total += Math.abs(number);

}

int rp = total % 100;

String info = null;

if (rp > 90) {

info = ("姓名:" + name + "\n人品为:" + rp + "\n评价:您的人品很好,祖坟冒青烟");

} else if (rp > 60) {

info = ("姓名:" + name + "\n人品为:" + rp + "\n评价:您的人品还不错,继续保持");

} else if (rp > 30) {

info = ("姓名:" + name + "\n人品为:" + rp + "\n评价:您的人品为渣...");

} else {

info = ("姓名:" + name + "\n人品为:" + rp + "\n评价:我的错,不该跟你提人品.");

}

tv_result.setText(info);

}

}

posted on 2017-05-04 21:27  导演丶  阅读(352)  评论(0)    收藏  举报

导航