美文网首页
Activity 页面回传数据

Activity 页面回传数据

作者: RoboyCore | 来源:发表于2016-11-04 15:06 被阅读28次

    页面一:
    private TextView tx1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tx1 = (TextView)findViewById(R.id.text2);
    }

    public void btnText(View view) {
        Intent intent = new Intent(this, Main2Activity.class);
    
    
        //传递数据 intent.putExtra();
        startActivityForResult(intent,998);
    }
    
    /**
     * 参数说明
     * @param requestCode 998,之前指定码
     * @param resultCode 结果码,结果页面传递过来的数据标记
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 998 && resultCode ==RESULT_OK) {
            //结果码是传递过来的页面,Result.OK ,Result.Cancel是按了onBackPressed
            String result = data.getStringExtra("result");
        tx1.setText(result);
        }
        if (requestCode == 998 && resultCode ==RESULT_CANCELED) {
    
            tx1.setText("cancel");
        }
    
    }
    

    页面二:
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        intent = new Intent(this, MainActivity.class);//创建意图
    
    }
    
    public void btnCallBack(View view) {
    
        //设置回传数据的
        intent.putExtra("result","这是结果");
    
        setResult(RESULT_OK,intent);//设置回传数据
        finish();//关闭页面
    
    }
    
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    

    相关文章

      网友评论

          本文标题:Activity 页面回传数据

          本文链接:https://www.haomeiwen.com/subject/qiqwuttx.html