Activity初级:startActivityForResult、重写onActivityResult、setResult回传数据、requestCode请求码

main端:

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
66
67
68
69
70
/**
 * 实现:主页面点击按钮1,跳转到页面二输入内容,点击页面二按钮返回主页面并显示输入信息;
 *     主页面点击按钮2,跳转到页面三输入内容,点击页面三按钮返回主页面并显示输入信息.
 * 主要步骤:
 * 1) startActivityForResult(intent, 0);
 * 2) 回传数据时,调用setResult(0, data)方法,然后执行finish()
 * 3) onActivityResult(int, int, Intent data)
 */
public class MainActivity extends Activity
{
    private static final int BTN_ONE = 1;//将常量封装定义
    private static final int BTN_TWO = 2;
                                  
    @Override//重写onActivityResult
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);//有参数的super必须重写
        TextView textView = (TextView) findViewById(R.id.textView1_main);
                                      
        if(requestCode == BTN_ONE)//根据请求码判断接收内容
        {
            String str = data.getStringExtra("one");
            textView.setText(str);
        }
        else if(requestCode == BTN_TWO)
        {
            String str = data.getStringExtra("two");
            textView.setText(str);
        }
                                      
    }
                                  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                                      
        //监听button1,跳转到NextActivity,等待result
        Button button1=(Button) findViewById(R.id.button1_main);
        button1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                startActivityForResult(intent, BTN_ONE);
            }
        });
                                      
        //监听button2,跳转到ThirdActivity,等待result
        Button button2 =(Button) findViewById(R.id.button2_main);
        button2.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
               Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
               startActivityForResult(intent, BTN_TWO);
            }
        });
    }
                                  
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


next端:

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
public class NextActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
                                   
        Button button =(Button) findViewById(R.id.button1_next);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //得到用户数据
                EditText editText=(EditText) findViewById(R.id.editText1_next);
                String strone = editText.getText().toString();
                                           
                //回传数据
                Intent data = new Intent();
                data.putExtra("one", strone);
                setResult(1, data );
                                           
                //关闭当前窗口
                finish();
            }
        });
    }
                               
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.next, menu);
        return true;
    }
}


third端:

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
public class ThirdActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
                                
        Button button = (Button) findViewById(R.id.button1_third);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                EditText editText = (EditText) findViewById(R.id.editText1_third);
                String strtwo = editText.getText().toString();
                Intent data = new Intent();
                data.putExtra("two", strtwo);
                setResult(2,data );
                finish();
            }
        });
    }
                            
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.third, menu);
        return true;
    }
}




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1195845,如需转载请自行联系原作者
上一篇:Spring Cloud实战小贴士:Zuul处理Cookie和重定向


下一篇:从零开始学习OI