zoukankan      html  css  js  c++  java
  • activity的切换及数据传递

    在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中更改主程序即可。

  • 相关阅读:
    POJ 3468 A Simple Problem with Integers(线段树区间求和)
    windows+Ubuntu双系统 windows引导修复
    CSDN博文大赛火爆开启
    TNS-12541,TNS-12560,TNS-00511,TNS-12542,TNS-12560,TNS-00512数据库启动监听报错
    MResource
    【LeetCode】【Python】Linked List Cycle
    中间件解析FDMEMTABLE.delta生成SQL的方法
    TDSTCPServerTransport 的Filters
    DATASNAP压缩过滤器的使用
    咏南CS插件开发框架也可BS方式部署
  • 原文地址:https://www.cnblogs.com/bycainiao/p/5192249.html
Copyright © 2011-2022 走看看