温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • Activity初级:startActivityForResult、重写onActivityResult、setResult回传数据、requestCode请求码

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

发布时间:2020-07-14 12:55:48 来源:网络 阅读:1224 作者:glblong 栏目:开发技术

main端:

/**
 * 实现:主页面点击按钮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端:

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端:

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;
    }
}




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI