显式意图切换界面
一。创建安卓项目
入口布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<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" > <Button android:id="@+id/btn_go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="进入下一个界面" android:onClick="goNextActivity"/> <Button android:id="@+id/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="进入Next界面" android:onClick="goNextActivity" android:layout_below="@id/btn_go"/> </RelativeLayout> |
二。新建界面
1.File,New,Other,Android Activity,命名为SecondActivity。
2.同时创建出布局文件
3.同时在Manifest.xml中自动注册
三。入口主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void goNextActivity(View v) { switch(v.getId()){ /** 显式意图:方式1*/ case R.id.btn_go: // 1.创建意图 Intent intent = new Intent(); // 2.显式意图,指定目标组件(目标组件所在的包,目标组件路径) intent.setClassName(getPackageName(), "com.cuiweiyou.intentiontest.SecondActivity"); /*隐式意图 intent.setAction(action); intent.setData(data);*/ // 3.实施意图 startActivity(intent); break; /** 显式意图:方式2*/ case R.id.btn_next: // 1.创建意图( 上下文,目标控件) Intent itt = new Intent(this, SecondActivity.class); // 2.实施 startActivity(itt); break; } } } |
隐式意图切换界面
一。入口布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<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" > <Button android:id="@+id/btn_setType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:onClick="goPage" android:text="隐式意图-打人-仅setType" /> <Button android:id="@+id/btn_setTypeAndSetData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_setType" android:layout_centerHorizontal="true" android:onClick="goPage2" android:text="隐式意图-打人-setType和setData" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_setTypeAndSetData" android:layout_centerHorizontal="true" android:onClick="goPage3" android:text="隐式意图-打开网页" /> </RelativeLayout> |
二。新界面的布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="使用隐式意图进入的新界面\n\n**被打的张三**" android:textSize="22dp" /> </RelativeLayout> |
三。新建界面类
1 2 3 4 5 6 7 8 9 10 11 |
public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 新界面使用新布局 setContentView(R.layout.activity_second); } } |
四。注册新界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cuiweiyou.implicitintention" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.cuiweiyou.implicitintention.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> <!-- 注册新建的界面2,意图的实施目标 --> <activity android:name="com.cuiweiyou.implicitintention.SecondActivity" > <intent-filter> <!-- 动作:打人 - 命名规范=包名.动作 --> <action android:name="com.cuiweiyou.implicitintention.beatPerson" /> <!-- 类型:打人的情况。什么时候打人 --> <category android:name="android.intent.category.DEFAULT" /> <!-- (动作目标)数据类型:被打的人群,一个群体 --> <data android:mimeType="type/xiaosan" /> </intent-filter> <!-- 仍然是上面的新建的界面2,新的意图过滤 --> <intent-filter> <action android:name="com.cuiweiyou.implicitintention.beatPerson" /> <category android:name="android.intent.category.DEFAULT" /> <!-- 不能自定义 --> <!-- (动作目标)数据类型:被打的人群,一个群体 --> <data android:mimeType="type/xiaosan" /> <!-- (动作目标)数据约束:被打人群中的一类。被打者的进一步过滤。自定义的约束名 --> <data android:scheme="jianren" /> <!-- “xiaosan”这个群体中的“jianren”这一类 --> </intent-filter> <!-- 仍然是上面的新建的界面2,新的意图 这里的配置和系统自带浏览器的配置相同。 第三方同类型应用会做这样的配置 --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" /> <data android:scheme="https" /> <data android:scheme="about" /> <data android:scheme="javascript" /> </intent-filter> </activity> </application> </manifest> |
六。主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void goPage(View v){ System.out.println("sssssssssssssssssssssssssssssssssss"); // 1.创建意图 Intent i = new Intent(); // 2.动作,打人。在Manifest.xml中配置的第二个界面对应动作 i.setAction("com.cuiweiyou.implicitintention.beatPerson"); // 3.类型,随时打。执行动作的情况 i.addCategory(Intent.CATEGORY_DEFAULT); // 对应Manifest.xml里的配置 // 4.数据类型,小三人群。实施意图的目标 i.setType("type/xiaosan"); startActivity(i); } public void goPage2(View v){ System.out.println("fffffffffffffffffffffffffffff"); Intent i = new Intent(); i.setAction("com.cuiweiyou.implicitintention.beatPerson"); //i.addCategory(Intent.CATEGORY_DEFAULT); i.addCategory("android.intent.category.DEFAULT"); // 4.如果需要同时设置mimeType和scheme。 // intent.setType("application/xiaosan"); // intent.setData(Uri.parse("jianren:阿美")); i.setDataAndType(Uri.parse("jianren://阿美"), "type/xiaosan"); startActivity(i); } public void goPage3(View v){ System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"); Intent i = new Intent(); i.setAction("android.intent.action.VIEW"); // 如果有第三方同类型应用配置相同,会弹出选择窗口 i.addCategory(Intent.CATEGORY_DEFAULT); // 默认状态 i.addCategory(Intent.CATEGORY_BROWSABLE); // 调用浏览器的时候 i.setData(Uri.parse("http://www.gaohaiyan.com")); startActivity(i); } } |
人品计算器
一。新建布局
二。新建界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public class CharacterActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_character); TextView tv_character = (TextView) findViewById(R.id.tv_character); // 1.获取打开本界面的意图 Intent intent = getIntent(); // 2.获取传入的参数 String name = intent.getStringExtra("name"); if (name != null) { // 上个界面已经进行了判断,这里是为了排除选择姓名按钮带来的意外 byte[] result = name.getBytes(); int number = 0; for (byte b : result) { number += b & 0xff; } int sorce = Math.abs(number) % 100; tv_character.setText(name + "人品值" + sorce); } // ------------------------ 发回人名 ------------------------------------ TextView tv_xiaoli = (TextView) findViewById(R.id.tv_xiaoli); tv_xiaoli.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView tv = (TextView) v; String name = tv.getText().toString(); Intent i = new Intent(); i.putExtra("name", name); setResult(0, i); finish(); } }); } /** * 通过控件的点击事件,发回人名 TextView控件需要设置 clickable="true" */ public void doResultName(View v) { TextView tv = (TextView) v; String name = tv.getText().toString(); System.out.println("看看这个名字:" + name); Intent i = new Intent(); i.putExtra("name", name); // 设置数据(回执代码,意图) setResult(0, i); // 关闭本界面,并回传数据给上层界面的Intent所在线程的onActivityResult。 finish(); } } |
三。注册界面
四。入口布局
五。入口界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public class MainActivity extends ActionBarActivity { private EditText et_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); } /** 人品计算 **/ public void doCalculation(View v){ String name = et_name.getText().toString(); if(TextUtils.isEmpty(name)){ Toast.makeText(this, "请输入一个名字", 0).show(); return; } // 1.显式意图 Intent i = new Intent(this, CharacterActivity.class); // 2.传参 i.putExtra("name", name); // 3.执行 startActivity(i); // 点击这个按钮进入到下一层界面,不回传数据 } /** 从子界面获取人名,回传 */ public void doGetName(View v){ // 1.显式意图 Intent i = new Intent(this, CharacterActivity.class); // 2.执行 // startActivity(i); // 如果需要下一个界面回传数据,使用此方法(意图,请求代码) startActivityForResult(i, 0); } /** * 当有界面回传数据时,会调用这个方法。 * requestCode:之前从本界面发出的意图的请求代码 * resultCode:下一层界面发回的意图的回执码 * data:一个存储有数据的意图对象 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // if (requestCode == 0){ // 如果由多个下层界面回传数据,这里可以先进行判断 // if (resultCode == 1){ if(data != null){ String name = data.getStringExtra("name"); et_name.setText(name); } // 最后执行super方法 super.onActivityResult(requestCode, resultCode, data); } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/208.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设