Android百日程序:使用Intent回传结果
扫描二维码
随时随地手机看文章
本章使用Intent,回传需要的结果;
流程:
启动主页->按下按键启动第二个activity->输入用户名,按下OK按键->回传用户名回主页->主页读取用户名并显示出来。
1 启动主页图:
2 启动第二个activity
3 输入用户名:
4 回传到主页,读取并显示:
新建项目,然后输入对应的代码就OK了。<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MSDW99KzvefD5rT6wusoYWN0aXZpdHlfbWFpbi54bWwpo7o8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">
建立一个Button,然后指定相应时间是onClick
2 主页的响应时间代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class MainActivity extends Activity { /* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */ int requestCode = 1 ; public static <t> boolean equ(T t1, T t2){ return t1 == t2; } @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult( new Intent( "su.bill.intenttest.SecondaryActivity" ), requestCode); } }</t> |
这里和一般的startActivity不用的就是使用了startActivityForResult,使用这个函数就能回传结果了。
3 进入第二个activity的界面代码:
?
1
2
3
4
5
6
|
<!--?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= "horizontal" > <edittext android:id= "@+id/txt_username" android:layout_weight= "1" android:layout_width= "0dp" android:layout_height= "wrap_content" android:hint= "@string/your_name" > </edittext></linearlayout> |
简单的一个输入文本,一个按键
4 第二个activity的逻辑代码
?
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
|
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class SecondaryActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.secondary_activity); } public void onClick(View v){ Intent data = new Intent(); EditText txt_username = (EditText) findViewById(R.id.txt_username); data.setData(Uri.parse(txt_username.getText().toString())); setResult(RESULT_OK, data); finish(); } } |
主要是onClick代码,新建一个Intent data之后,设置好这个Intent的内容,然后利用setResult回传这个Intent给主页activity,其实基本原理还是利用Intent通信。
5 主页逻辑的读取并显示结果的代码:
?
1
2
3
4
5
6
|
@Override public void onActivityResult( int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText( this , data.getDataString(), Toast.LENGTH_SHORT).show(); } } |
从上一个对应的activity返回的时候就自动调用处理的函数,其中的表示符号就是requestCode和返回的标识符reqCode对应起来,以便确认这个返回的Intent是对应的activity返回的。
主页逻辑的完整代码如下:
?
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
|
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { /* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */ int requestCode = 1 ; public static <t> boolean equ(T t1, T t2){ return t1 == t2; } @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult( new Intent( "su.bill.intenttest.SecondaryActivity" ), requestCode); } @Override public void onActivityResult( int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText( this , data.getDataString(), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true ; } return super .onOptionsItemSelected(item); } } </t> |
其他没说到的代码都是新建项目的时候自动生成的代码了,可以暂时忽略。
就是熟悉好Android的一些机制,理解起来倒是没有什么难度的。