【问题标题】:Carrying double value throughout classes在整个课程中进行双重价值
【发布时间】:2014-04-14 01:21:00
【问题描述】:

我会尽量减少信息,但这个应用程序应该在您每次按下单选按钮时计算一个分数。有 4 个带有单选按钮的屏幕,然后第五个屏幕显示您的分数。

不过,计算总是到数字 4。无论按什么,都没有任何添加。我哪里错了?

    RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);
    RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
    RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
    RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);       

    //Calculation based on GPA question
    if (gpa1.isChecked()){calculation = calculation + 1;}
    else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
    else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
    else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
    else{calculation = 0.0; }}}}

这是第一个主要课程,在他们选择答案并按下下一步按钮后,它会进入第二页,其中包含另一个问题和另外 4 个答案,这些答案也应该添加到计算变量中。

这里有更多的代码来显示完全涵盖的内容:

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    //RESTORES SAVED STATES
    if (savedInstanceState == null ){calculation = 0.0;}
    else{calculation = savedInstanceState.getDouble(CALCULATION);}

    addListenerOnButton();

    RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);
    RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
    RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
    RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);       

    //Calculation based on GPA question
    if (gpa1.isChecked()){calculation = calculation + 1;}
    else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
    else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
    else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
    else{calculation = 0.0; }}}}

    //Get button to do button stuff like go to the next page
    Button butGPA = (Button) findViewById(R.id.butGPA);
    butGPA.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            Intent i = new Intent(Main.this, Main2.class);
            startActivity(i);
        }
    });


    }

【问题讨论】:

  • 您的if 声明在哪里?是在onCreate()吗?
  • 是的。我试图减少我在这里输入的信息量,因为我的最后一个问题没有引起太多关注
  • 那是你的问题。看我的回答

标签: java android


【解决方案1】:

由于您的if 语句在onCreate() 中,因此该值始终是RadioButton,默认情况下会检查它显然是第一个,这就是为什么您总是得到4 的值。移动

 if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}

到一些事件,例如Button onClick() 或任何你用来移动到下一个Activity 的东西。您也可以在 onCheckedChangeListener() 中设置值。

记住onCreate()只有会在Activity 被销毁后第一次启动时被调用。因此,您放入其中的任何代码(除非在某些事件侦听器中)都不会从 onCreate() 中更改。

编辑

butGPA.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
         if (gpa1.isChecked()){calculation = calculation + 1;}
         else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
         else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
         else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
         else{calculation = 0.0; }}}}
        Intent i = new Intent(Main.this, Main2.class);
        i.putExtra("calc", calculation);
        startActivity(i);
    }
});

像这样将值传递给每个Activity 然后获取值(在onCreate() 中或之后)执行类似的操作

Intent i = getIntent();
double calc = i.getDoubleExtra("calc", 0.0);

假设变量最初是double,尽管我不确定为什么它不是int。您可能想检查 null 之类的东西,但无论如何这应该可以帮助您入门。

【讨论】:

  • 好的,我找到了一个教程,显示使用“addListenerOnButton()”来监听它,这行得通吗?
  • 当然可以,但您可以使用onCheckedChange() 并在那里设置值。或者,正如我所说,将此代码放在 onClick() 或您已经使用的任何内容中,以转到下一个 Activity
  • 你能帮忙解释一下,但给我看一些代码示例吗?另外,我真的很感谢你的帮助。我对此很陌生。
  • 我假设您说要使用公共 void onClick()?
  • 我知道你不是在寻找积分,只是想指出你是个好人。谢谢!我完成了任务,现在它就像一个魅力。 =)
【解决方案2】:

你可以使用

 newIntent.putExtra("varname",value);

设置属性值。并且可以获得新意图的价值

getStringExtra("varname");

请参考这个问题:How do you pass data/parameters to another activity in Android。它可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 2020-04-17
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多