1.开启新的Activity的方法:

(1)Intent 意图

(2)intent.setAction("自定义")  记得在清单文件中声明

(3)intent.setData(前缀) //设置数据,记得在清单文件中声明

(4)startActivity(intent);

(5)在跳转之后新的界面Activity中,使用getIntent()方法获得开启当前Activity的Intent;

(6)之后可以用第二界面获得由第一个界面Activity传递过来的数据,getIntent().getData().toString();

2.人品计算器案例:

(1)新建一个人品计算器的Android工程,首先设置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" > <Button
android:id="@+id/bt_start"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击开始使用" /> <TextView
android:layout_below="@id/bt_start"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="根据八卦原理开发的人品计算器" /> <EditText
android:id="@+id/et_name"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:hint="请输入要测试的人的姓名"
android:layout_above="@id/bt_start"
/> </RelativeLayout>

布局效果如下:

(2)接下来编写主界面MainActivity,完成界面跳转的逻辑如下:

 package com.itheima.rpcalc;

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity {
private Button bt_start;
private EditText et_name; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_start = (Button) findViewById(R.id.bt_start);
et_name = (EditText) findViewById(R.id.et_name);
bt_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到第二个界面
Intent intent = new Intent();
intent.setAction("com.itheima.rpcalc.CALC");
intent.setData(Uri.parse("calc://"+et_name.getText().toString()));
startActivity(intent);
}
});
}
}

当我们点击这里按钮Button,会通过Intent的action定位到相应的CalcActivity,通过intent启动这个CalcActivity,intente携带数据也会传递给CalcActivity。注意我们这里的intent设置的Action和Data都是自定义的,Android系统是无法识别的,所以我们必须实现注册这些信息;

在AndroidMainfest.xml文件中注册这些信息,如下:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.rpcalc"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.rpcalc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 声明一个新的界面 --> <activity android:name="com.itheima.rpcalc.CalcActivity"> <intent-filter >
<!-- 注册一个action-->
<action android:name="com.itheima.rpcalc.CALC"/>
<!-- 注册一个数据前缀,这里是calc,则是数据前缀为:calc://…… -->
<data android:scheme="calc"/>
<!-- 声明CalcActivity运行的环境为手机(默认) -->
<category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
</application> </manifest>

 这里我们在<intent-filter >里面必须注册action 和 categroy的信息;其他的数据信息,在具体情况具体分析。

(3)接下来完成第二个界面的布局文件activity_calc.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" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算结果:"
android:textSize="20sp"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_result"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的人品为:85分"
android:textColor="#88ff0000" /> </LinearLayout>

布局效果如下:

(4)编写CalcActivity,如下:

 package com.itheima.rpcalc;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class CalcActivity extends Activity {
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
tv_result = (TextView) findViewById(R.id.tv_result); Intent intent = getIntent();//getIntent()获得开启当前CalcActivity的intent,也就是第一个MainActivity中传递给CalcActivity的intent
String data = intent.getData().toString();
String name = data.replace("calc://", "");
//计算人品了。 byte[] result = name.getBytes();
int total = 0 ;
for(byte b:result){
//byte是8位二进制数据,而0xff是属于java的字面常量,它属于int类型,这里b&0xff,将byte类型数据转换为int
//byte ---> int
int number = b&0xff;
total+=number;
}
int rp = Math.abs(total)%100;
String desc="";
if(rp>90){
desc = "你的人品非常好,祖坟冒青烟";
}else if(rp > 60){
desc = "你的人品还不错";
}else if(rp>30){
desc = "人品比较糟糕,3岁偷看大妈洗澡";
}else{
desc = "人品掉渣了。";
} tv_result.setText(name+"的人品为:"+rp+"\n"+desc);
}
}

布署程序到模拟器上如下:

Android(java)学习笔记218:开发一个多界面的应用程序之人品计算器的简单实现的更多相关文章

  1. Android(java)学习笔记161:开发一个多界面的应用程序之人品计算器的简单实现

    1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义")  记得在清单文件中声明 (3)intent.setData(前 ...

  2. Android:日常学习笔记(8)———开发微信聊天界面

    Android:日常学习笔记(8)———开发微信聊天界面 只做Nine-Patch图片 Nine-Patch是一种被特殊处理过的PNG图片,能够指定哪些区域可以被拉升,哪些区域不可以.

  3. Android(java)学习笔记219:开发一个多界面的应用程序之两种意图

    1.两种意图: (1)显式意图: 在代码里面用intent设置要开启Activity的字节码.class文件: (2)隐式意图: Android(java)学习笔记218:开发一个多界面的应用程序之人 ...

  4. Android(java)学习笔记162:开发一个多界面的应用程序之两种意图

    1.两种意图: (1)显式意图: 在代码里面用intent设置要开启Activity的字节码.class文件: (2)隐式意图: Android(java)学习笔记218:开发一个多界面的应用程序之人 ...

  5. Android(java)学习笔记222:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  6. Android(java)学习笔记221:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

    1.首先我们看看下面这个需求: 这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到" ...

  7. Android(java)学习笔记165:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  8. Android(java)学习笔记164:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

    1.首先我们看看下面这个需求: 这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到" ...

  9. Android(java)学习笔记217:开发一个多界面的应用程序之清单文件

    清单文件的重要参数:     <intent-filter>             代表的应用程序的入口界面           <action android:name=&quo ...

随机推荐

  1. Js Framework

    http://www.mhtml5.com/2012/06/5119.html http://www.mhtml5.com/2012/06/5118.html http://cubiq.org/isc ...

  2. cf B Inna and Candy Boxes

    题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...

  3. ibatis动态语句加and 和不加and

    <select id="queryGoodsGroupCodeName" parameterClass="String" resultClass=&quo ...

  4. GPIO软件模拟I2C

    /***************************************************************************** * * Filename: * ----- ...

  5. 【HDOJ】3459 Rubik 2×2×2

    模拟+DFS. /* 3459 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...

  6. find the mincost route(floyd变形 无向图最小环)

    Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. Eqs (哈希)

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10695   Accepted: 5185 Description Cons ...

  8. MongoDB主从配置

    master的配置 # cat mongod.conf dbpath = /app/sinova/mongodata/db            #指定数据库目录 logpath = /app/sin ...

  9. 【模拟】Codeforces 691B s-palindrome

    题目链接: http://codeforces.com/problemset/problem/691/B 题目大意: 求一个字符串是不是镜像的(不是回文).是输出TAK否则RE. 题目思路: [模拟] ...

  10. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...