将数据从活动1传递到2,然后从2传递到1

时间:2016-09-17 17:11:27

标签: android android-studio android-intent

我是初学Android开发者,我一直坚持这个问题。 活动1-:我从一个按钮开始另一个活动,并将一些数据传递给它。 活动2-:我正在做一些事情并将一些数据传递给第一个活动。

CHEAT ACTIVITY不会打开,而它会发回我想要的值。虽然我看不到活动,但我得到了结果。

完整代码:

MainActivity.java

 public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 1 ;
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private TextView textView;
    private TextView textView3;
    int i = 0;
    int n;
    int flag = 0;
    int k;
    int random;
    private final String CHEAT_KEY="CHEAT_KEY";


      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4=(Button) findViewById(R.id.button4);
        button5=(Button) findViewById(R.id.button5);
        textView = (TextView) findViewById(R.id.textView);





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

            if (k == 0)
           Toast.makeText(MainActivity.this, "Incorrect",           Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                k = isprime();
                if (k == 0)
                    Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(MainActivity.this, "Incorrect", Toast.LENGTH_SHORT).show();
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                n = looping();
//                String numb = String.valueOf(n);
                random = looping();
                k = IsPrime(random);
                String question = "Is it Prime-" + random;
                textView.setText(question);
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,CheatActivity.class);
                intent.putExtra("CHEAT_KEY",k);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK && requestCode== REQUEST_CODE){

            if (data.hasExtra("MY_DATA")){
                Toast.makeText(this, data.getExtras().getString("MY_DATA"), Toast.LENGTH_SHORT).show();
            }

        }
    }



    public int looping() {
        Random r = new Random();
//        for(i=1;i<=100;i++){
//            n= r.nextInt(100 - 1 + 1) + 1;
//            return n;
//        }
        return r.nextInt(100 - 1 + 1) + 1;
    }



    int IsPrime(int number) {
        if (number <= 1)
            return 0;
        int i;
        for (i=2; i*i<=number; i++) {
            if (number % i == 0)
                return 0;
        }
        return 1;
    }

    public void exit(View view) {
        finish();
        System.exit(0);
    }
}

CheatActivity.java

public class CheatActivity extends AppCompatActivity {


    TextView textView2;
    private String answer;
    private final String MY_DATA="MY_DATA";


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

        textView2=(TextView) findViewById(R.id.textView2);
        int get=getIntent().getExtras().getInt("CHEAT_KEY");

        if(get==1)
        {
            textView2.setText("TRUE");

        }
        if (get==0)
        {
            textView2.setText("FALSE");

        }

        Intent data=new Intent();
        data.putExtra("MY_DATA","User has cheated!");
        setResult(RESULT_OK, data);
        finish();
    }
}

0 个答案:

没有答案