1+ < ?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns ="http://www.w3.org/1999/xhtml "> < head > < meta http-equiv ="Content-Type " content ="text/html; charset=UTF-8 " /> < title > 34.4. </ title > < link rel ="stylesheet " type ="text/css " href ="..//docbook.css " /> < meta name ="generator " content ="DocBook XSL Stylesheets V1.79.1 " /> < link rel ="home " href ="../index.html " title ="Netkiller Java 手札 " /> < link rel ="up " href ="android.layout.html " title ="第 34 章 Layout " /> < link rel ="prev " href =".html " title ="34.3. Activity 间数据传递 " /> < link rel ="next " href ="ListView.html " title ="34.5. ListView " /> </ head > < body > < a xmlns ="" href ="//www.netkiller.cn/ "> Home</ a > |
3+ < a xmlns ="" href ="//netkiller.github.io/ "> 简体中文</ a > |
4+ < a xmlns ="" href ="http://netkiller.sourceforge.net/ "> 繁体中文</ a > |
5+ < a xmlns ="" href ="/journal/index.html "> 杂文</ a > |
6+ < a xmlns ="" href ="//www.netkiller.cn/home/donations.html "> 打赏(Donations)</ a > |
7+ < a xmlns ="" href ="https://yq.aliyun.com/u/netkiller/ "> 云栖社区</ a > |
8+ < a xmlns ="" href ="http://my.oschina.net/neochen/ "> OSChina 博客</ a > |
9+ < a xmlns ="" href ="https://www.facebook.com/bg7nyt "> Facebook</ a > |
10+ < a xmlns ="" href ="http://cn.linkedin.com/in/netkiller/ "> Linkedin</ a > |
11+ < a xmlns ="" href ="https://zhuanlan.zhihu.com/netkiller "> 知乎专栏</ a > |
12+ < a xmlns ="" href ="https://github.com/netkiller "> Github</ a > |
13+ < a xmlns ="" href ="/search.html "> Search</ a > |
14+ < a xmlns ="" href ="//www.netkiller.cn/home/about.html "> About</ a > < div class ="navheader "> < table width ="100% " summary ="Navigation header "> < tr > < th colspan ="3 " align ="center "> 34.4. </ th > </ tr > < tr > < td width ="20% " align ="left "> < a accesskey ="p " href =".html "> 上一页</ a > </ td > < th width ="60% " align ="center "> 第 34 章 Layout</ th > < td width ="20% " align ="right "> < a accesskey ="n " href ="ListView.html "> 下一页</ a > </ td > </ tr > </ table > < hr /> </ div > < table xmlns =""> < tr > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large " height ="30 " width ="170 " frameborder ="0 " scrolling ="0 " style ="width:170px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large " height ="30 " width ="170 " frameborder ="0 " scrolling ="0 " style ="width:170px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large " height ="30 " width ="240 " frameborder ="0 " scrolling ="0 " style ="width:240px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > </ tr > </ table > < div class ="section "> < div class ="titlepage "> < div > < div > < h2 class ="title " style ="clear: both "> < a id =""> </ a > 34.4. </ h2 > </ div > </ div > </ div >
15+
16+ < pre class ="programlisting ">
17+
18+ public class MainActivity extends AppCompatActivity {
19+
20+ //我们需要自己写一个常量作为requestCode,在请求result时传递进去
21+ public static final int REQUEST_CODE_NORMAL = 100;
22+
23+ @Override
24+ protected void onCreate(Bundle savedInstanceState) {
25+ super.onCreate(savedInstanceState);
26+ setContentView(R.layout.activity_main);
27+
28+ Button button = (Button) findViewById(R.id.Button);
29+
30+ button.setOnClickListener(new View.OnClickListener() {
31+ public void onClick(View view) {
32+ startActivityForResult(new Intent(this,SecondActivity.class),REQUEST_CODE_NORMAL);
33+ }
34+ });
35+ }
36+
37+ @Override
38+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
39+ super.onActivityResult(requestCode, resultCode, data);
40+ if (requestCode == REQUEST_CODE_NORMAL) {
41+ //获得Result数据并处理
42+ ...
43+ ...
44+ }
45+ }
46+ }
47+
48+ </ pre >
49+ < pre class ="programlisting ">
50+
51+ public class SecondActivity extends AppCompatActivity {
52+
53+ @Override
54+ protected void onCreate(Bundle savedInstanceState) {
55+ super.onCreate(savedInstanceState);
56+ setContentView(R.layout.save);
57+
58+ Button button = (Button) findViewById(R.id.SaveButton);
59+
60+ button.setOnClickListener(new View.OnClickListener() {
61+ public void onClick(View view) {
62+ Intent intent = new Intent(this,MainResultActivity.class);
63+ intent.putExtra("content",etContent.getText().toString());
64+ setResult(1,intent);
65+ //发送Result数据给请求方,然后finish()
66+ finish();
67+ }
68+ });
69+ }
70+ }
71+
72+ </ pre >
73+ </ div > < div xmlns ="" id ="disqus_thread "> </ div > < script xmlns ="">
74+
75+ var disqus_config = function ( ) {
76+ this . page . url = "http://www.netkiller.cn" ; // Replace PAGE_URL with your page's canonical URL variable
77+ this . page . identifier = 'netkiller' ; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
78+ } ;
79+
80+ ( function ( ) { // DON'T EDIT BELOW THIS LINE
81+ var d = document , s = d . createElement ( 'script' ) ;
82+ s . src = '//netkiller.disqus.com/embed.js' ;
83+ s . setAttribute ( 'data-timestamp' , + new Date ( ) ) ;
84+ ( d . head || d . body ) . appendChild ( s ) ;
85+ } ) ( ) ;
86+ </ script > < noscript xmlns =""> Please enable JavaScript to view the < a href ="https://disqus.com/?ref_noscript "> comments powered by Disqus.</ a > </ noscript > < br xmlns ="" /> < script xmlns ="" type ="text/javascript " id ="clustrmaps " src ="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM "> </ script > < div class ="navfooter "> < hr /> < table width ="100% " summary ="Navigation footer "> < tr > < td width ="40% " align ="left "> < a accesskey ="p " href =".html "> 上一页</ a > </ td > < td width ="20% " align ="center "> < a accesskey ="u " href ="android.layout.html "> 上一级</ a > </ td > < td width ="40% " align ="right "> < a accesskey ="n " href ="ListView.html "> 下一页</ a > </ td > </ tr > < tr > < td width ="40% " align ="left " valign ="top "> 34.3. Activity 间数据传递 </ td > < td width ="20% " align ="center "> < a accesskey ="h " href ="../index.html "> 起始页</ a > </ td > < td width ="40% " align ="right " valign ="top "> 34.5. ListView</ td > </ tr > </ table > </ div > < script xmlns ="">
87+ ( function ( i , s , o , g , r , a , m ) { i [ 'GoogleAnalyticsObject' ] = r ; i [ r ] = i [ r ] || function ( ) {
88+ ( i [ r ] . q = i [ r ] . q || [ ] ) . push ( arguments ) } , i [ r ] . l = 1 * new Date ( ) ; a = s . createElement ( o ) ,
89+ m = s . getElementsByTagName ( o ) [ 0 ] ; a . async = 1 ; a . src = g ; m . parentNode . insertBefore ( a , m )
90+ } ) ( window , document , 'script' , '//www.google-analytics.com/analytics.js' , 'ga' ) ;
91+
92+ ga ( 'create' , 'UA-11694057-1' , 'auto' ) ;
93+ ga ( 'send' , 'pageview' ) ;
94+
95+ </ script > < script xmlns ="" async ="async ">
96+ var _hmt = _hmt || [ ] ;
97+ ( function ( ) {
98+ var hm = document . createElement ( "script" ) ;
99+ hm . src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c" ;
100+ var s = document . getElementsByTagName ( "script" ) [ 0 ] ;
101+ s . parentNode . insertBefore ( hm , s ) ;
102+ } ) ( ) ;
103+ </ script > < script xmlns ="" async ="async ">
104+ ( function ( ) {
105+ var bp = document . createElement ( 'script' ) ;
106+ var curProtocol = window . location . protocol . split ( ':' ) [ 0 ] ;
107+ if ( curProtocol === 'https' ) {
108+ bp . src = 'https://zz.bdstatic.com/linksubmit/push.js' ;
109+ }
110+ else {
111+ bp . src = 'http://push.zhanzhang.baidu.com/push.js' ;
112+ }
113+ var s = document . getElementsByTagName ( "script" ) [ 0 ] ;
114+ s . parentNode . insertBefore ( bp , s ) ;
115+ } ) ( ) ;
116+ </ script > < script xmlns ="" type ="text/javascript " src ="/js/q.js " async ="async "> </ script > </ body > </ html >
0 commit comments