Android Intent不会从Switch传递价值

时间:2013-11-27 09:34:48

标签: android android-intent switch-statement

我有一个Switch按钮,我想从中将不同的值(1或2)传递给下一个活动进行计算。但我总是得到0.任何帮助?

 Switch mySwitch = (Switch) findViewById(R.id.edit_home);

     // set the switch to ON
     mySwitch.setChecked(true);
     // attach a listener to check for changes in state
     mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

     if (isChecked) {

     double input5 = 2;  
     Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
     intentx.putExtra("IFValue",input5);
             startActivity(intentx);
     Toast.makeText(getApplicationContext(), "Home field is important advantage",
     Toast.LENGTH_SHORT).show();

     } else {

     double input5 = 1;
     Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
     intenty.putExtra("IFValue",input5);
             startActivity(intentx);
     Toast.makeText(getApplicationContext(),
     "Home field is not advantage", Toast.LENGTH_SHORT).show();
     }

     }
     });

在下一个活动中:

     Bundle bundle = getIntent().getExtras();
     double input5 = bundle.getDouble("IFValue");

编辑:我想将值(input5)发送到下一个活动,但是如果我在switch中添加startActivity(intentx);然后立刻去那里,而不是当我按下按钮(buttonForward)时,我想要。

其余代码:

    Switch mySwitch = (Switch) findViewById(R.id.edit_home);

     // set the switch to ON
     mySwitch.setChecked(true);
     // attach a listener to check for changes in state
     mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

     if (isChecked == true) {

     double input5 = 2;  
     Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
     intentx.putExtra("IFValue",input5);
     startActivity(intentx);
     Toast.makeText(getApplicationContext(), "Home field is important advantage",
     Toast.LENGTH_SHORT).show();

     } else {

     double input5 = 1;
     Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
     intenty.putExtra("IFValue",input5);
     startActivity(intenty);
     Toast.makeText(getApplicationContext(),
     "Home field is not advantage", Toast.LENGTH_SHORT).show();
     }

     }
     });



    Button buttonForward = (Button) findViewById(R.id.buttonToMain2);
    buttonForward.setOnClickListener(new OnClickListener(){


        @Override
        public void onClick(View arg0) {

    EditText edit_team = (EditText) findViewById(R.id.edit_team);
    EditText edit_form = (EditText) findViewById(R.id.edit_form);

    EditText edit_import = (EditText) findViewById(R.id.edit_import);

    String ekipa1 = edit_team.getText().toString();

    final double input2 = Double.valueOf(qualityControl.getProgress());
    final double input3 = Double.valueOf(edit_form.getText().toString());
        final double input4 = Double.valueOf(ratingBar.getRating()); 

    final double input6 = Double.valueOf(edit_import.getText().toString());

    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",input2);
    intent.putExtra("Value1",input3);
    intent.putExtra("Value2",input4);

    intent.putExtra("Value4",input6);
    intent.putExtra("team1", ekipa1);
    startActivity(intent);








        }



    });     

1 个答案:

答案 0 :(得分:2)

您正在侦听器中创建Intent(intentx& intenty)的本地实例(我没有看到您在侦听器中调用startActivity())。传递的Extras仅适用于本地副本,而不适用于您使用startActivity()的侦听器外使用的副本。

更新1

使用Extra键时字符大小写:

您正在使用intentx.putExtra("IFvalue",input5);

在下一个活动中,您使用的是double input5 = bundle.getDouble("IFValue");,这是一个不同的密钥。

更新2

  

我想将值(input5)发送到下一个活动,但是如果我添加   switch内部的startActivity(intentx);然后立即去那里   而不是按我想按下下一个按钮(buttonForward)。

然后,您需要在整个Intent中只有一个Activity实例(在onCreate()之前定义它,然后不要从切换侦听器启动Activity ,但为单个Extra实例添加Intent

Intent intent = new Intent(MainActivity.this,Main2Activity.class); // Class level variable

然后在切换侦听器中更改以下内容:

 // Intent intentx=new Intent(MainActivity.this,Main2Activity.class); remove this
 intent.putExtra("IFValue",input5);

并在buttonForward中单击侦听器:

//Intent intent=new Intent(MainActivity.this,Main2Activity.class); remove this
intent.putExtra("Value",input2);
intent.putExtra("Value1",input3);
intent.putExtra("Value2",input4);

intent.putExtra("Value4",input6);
intent.putExtra("team1", ekipa1);
startActivity(intent);