zoukankan      html  css  js  c++  java
  • 返回数据到前一个activity

    在主程序中 调用activity2的方法改为startActivityForResult(intent,0) 0代表下一个Activity要返回值的依据,可指定为自行定义的参考标示符。程序重写onactivityresult()这个方法,使程序在收到result后,重写加载写回原先输入的值。(在AS中重写结果报错,我删去了@override)

    activity主程序的代码

    package com.administrator.testall;
    
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.v7.app.ActionBarActivity;
    import android.content.Intent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    
    public class ChangeTest01 extends ActionBarActivity {
    
        private EditText et;
        private RadioButton rg1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_change_test01);
    
            Button button01 = (Button) findViewById(R.id.button1);
            button01.setOnClickListener(new Button.OnClickListener()
            {
                                           public void onClick(View v)
                                           {
                                               EditText et = (EditText) findViewById(R.id.editText1);
                                               double editText1 = Double.parseDouble(et.getText().toString()); //取值
    
                                               String choose = "";
                                               RadioButton rg1 = (RadioButton) findViewById(R.id.radioButton); //取复选框值
                                               if(rg1.isClickable())
                                               {
                                                   choose = "M";
                                               }
                                               else choose = "F";
    
                                               Intent intent = new Intent();
                                               intent.setClass(ChangeTest01.this,ChangeTest02.class); //activity却换
    
                                               Bundle bundle = new Bundle();  //activity的数据传递
                                               bundle.putDouble("edit1",editText1); //引号内为传递数据,引号后面为传递值
                                               bundle.putString("choose",choose);
    
                                               intent.putExtras(bundle); //将bundle对象分配给intert;
                                               startActivityForResult(intent, 0);    //调用另一个activity
                                           }
                                        }
            );
        }
    
        protected void OnActivityResult(int requestCode,int resultCode,Intent data)
        {
            switch (resultCode)
            {
                case RESULT_OK:
                    //取得来自activity2的数据 显示在画面上
                    Bundle bundle = data.getExtras();
                    String choose = bundle.getString("choose");
                    double edit1 = bundle.getDouble("edit1");;
    
                    et.setText(String.valueOf(edit1));
                    if(choose.equals("M")) rg1.setChecked(true);
                    break;
                default:
                    break;
            }
        }
    }

    注意 这里不需要使用@Override重写

    activity2的代码如下

    package com.administrator.testall;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.widget.Button;
    import android.widget.TextView;
    import android.view.View;
    
    public class ChangeTest02 extends ActionBarActivity {
    
        Bundle bundle;
        Intent intent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_change_test02);
    
            intent = this.getIntent();
            bundle = this.getIntent().getExtras(); //取得intent中的bundle对象
    
            String choose = bundle.getString("choose"); //取得bundle对象中的数据
            double edit1 = bundle.getDouble("edit1");
    
            String chooseText = "";
            if(choose.equals("M"))
            {
                chooseText="A";
            }else chooseText="B";
    
            TextView textView1 = (TextView) findViewById(R.id.textView1);
            textView1.setText(chooseText);
            TextView textView2 = (TextView) findViewById(R.id.textView2);
            textView2.setText(String.valueOf(edit1)); //double转string
    
            Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new Button.OnClickListener()
            {
                                          public void onClick(View v)
                                          {
                                              ChangeTest02.this.setResult(RESULT_OK,intent); //返回result到上一个activity
                                              ChangeTest02.this.finish();  //结束这个activity
                                          }
                                       }
            );
        }
    
    }
  • 相关阅读:
    试题 历届试题 整数拼接(数位dp)
    试题 历届试题 波动数列(dp)
    序列化二叉树(模拟)
    对于maven中无法加载类路径下的配置文件
    [蓝桥杯][算法训练]文章翻转
    [蓝桥杯][算法训练] 无权最长链
    [蓝桥杯][基础训练]2n皇后问题
    蓝桥杯 基础练习——Huffuman树
    蓝桥杯 基础练习——高精度加法
    蓝桥杯 基础练习——阶乘计算
  • 原文地址:https://www.cnblogs.com/bycainiao/p/5192393.html
Copyright © 2011-2022 走看看