简体   繁体   English

Android:如何对包/意图值使用if语句?

[英]Android: How to use if statement for bundle/intent values?

so a quick question. 一个简单的问题。 In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. 在我的应用程序中,用户经历了多个活动,为他们提供了单选按钮供您选择..在最后一个活动中,根据那里的选项,将显示他们所处的角色等。现在的问题是我不知道不知道如何编写代码来做到这一点。 Here is what I have 这是我所拥有的

First activity 第一次活动

public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle = getIntent().getExtras();

                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg1", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

Second activity 第二次活动

Button btn;
RadioGroup rg2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz2);


    btn = (Button) findViewById(R.id.nextBtn2);
    rg2= (RadioGroup) findViewById(R.id.rg2);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg2.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent = new Intent(getApplicationContext(), Quiz3.class);
                Bundle bundle = getIntent().getExtras();
                int id = rg2.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg2", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

This continues for about 7 activities 持续进行约7项活动

Final activity (where the result and the character are shown) 最终活动(显示结果和角色)

public class Final1 extends Activity {

Button btnRestart;
Button btnShare;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView1.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView2.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView3.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView4.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView5.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView6.setText(bundle.getCharSequence("rg6"));

    btnRestart = (Button)findViewById(R.id.restartBtn);

    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

    btnShare = (Button)findViewById(R.id.btnShare);

    btnShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "check out this app";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));


        }
    });


  }

}

Now, in the final activity, I want to have a code where if for example: 现在,在最后的活动中,我想要一个代码,例如:

if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected), 如果在activity1(Quiz1)中选择了选项1或2(如在选中的单选按钮中一样),

Quiz2: options 2 or 4 测验2:选项2或4

Quiz3: options 1 or 4 测验3:选项1或4

Quiz2: options 2 or 3 测验2:选项2或3

and so on... 等等...

then change a textview to something specific like "your character is x" 然后将textview更改为特定的内容,例如“您的字符是x”

I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple. 我已经将所有信息带到了最后一堂课,尽管听起来很简单,但我只是不知道如何解决这个问题。

Any help would be appreciated, thank you <3 任何帮助,将不胜感激,谢谢您<3

EDIT: 编辑:

TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
    if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
            && bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
            || bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
            && bundle.getString("rg6").equals("Yes"))) {
        textviewResult.setText("x");

    }else{
        textviewResult.setText("not x");
    }

The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x) 这样做的问题是,即使我为rg选择了另一个选项(因此也没有选择“ A”或“ B”选项),但是对于其余部分,我选择了If语句中的选项,但最终还是说x(但是应该说不是x)

You can use the String equals() method inside your if statement. 您可以在if语句中使用String equals()方法。 It returns true if both Strings are equal. 如果两个字符串相等,则返回true。

Eg: 例如:

if(bundle.getString("rg").equals("YourRadioButtonText")){ ... }

Your code could look something like this: 您的代码可能看起来像这样:

    `public class Final1 extends Activity {

Button btnRestart;
Button btnShare;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView1.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView2.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView3.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView4.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView5.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView6.setText(bundle.getCharSequence("rg6"));

    // NEW

TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
        textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
        textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
        textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
        textviewResult.setText("It is not A");
}
else {
    textviewResult.setText("Mhhh");
}

// END



    btnRestart = (Button)findViewById(R.id.restartBtn);

    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

    btnShare = (Button)findViewById(R.id.btnShare);

    btnShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "check out this app";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));


        }
    });


  }

}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM