码迷,mamicode.com
首页 > 其他好文 > 详细

activity的切换及数据传递

时间:2016-02-16 12:58:15      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

在antivity1中 运用intent及bundle对象,切换activity以及传递数据

 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;
                                           startActivity(intent);    //调用另一个activity
                                       }
                                    }
        );

而在activity2中 接受bundle对象中的数据

        Bundle 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    

这里需要注意的是 因为超过了一个activity,所以我们需要决定主程序;

在AndroidMainfest.xml中

设置

        <activity
            android:name=".ChangeTest01"
            android:label="@string/title_activity_change_test01" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ChangeTest02"
            android:label="@string/title_activity_change_test02" >
        </activity>

如图 changetest01中 因为添加了entry point 所以从它先开始启动

android studio与esplice中不同,不需要手动添加activity。

如果需要 只要在AndroidManifest中更改主程序即可。

activity的切换及数据传递

标签:

原文地址:http://www.cnblogs.com/bycainiao/p/5192249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!