意图在不同活动之间切换时无法正常工作

时间:2013-02-05 11:02:45

标签: android android-intent toggle togglebutton

我已经用5个活动做了一个应用程序--calculator1.java,calculator2.java,deposit.java,calculator3.java,calculator4.java,在活动1中有一个切换按钮和一个下一个按钮,以及所有其他活动包含一个下一个按钮,现在我想要的是当按下切换按钮并按下下一个按钮然后它应该转到calculator2.java,并且当我按下按钮时,如果calculator.java的“toggleButton”是“在“它应该去deposit.java否则转到calculator3.java,

我的代码如下:(点击下一个按钮):

Calculator1.java

btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Intent intent = new Intent(Calculator_1Activity.this, Calculator2.class);
                    intent.putExtra("toggleBtn", tg.isChecked());
                    startActivity(intent);
            }
        });

Calculator2.java

 btn2.setOnClickListener(new OnClickListener() {
             final boolean val= getIntent().getExtras().getBoolean("tg");
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if(val==true){
                        Intent iq=new Intent(Calculator2.this,Deposit.class);
                        startActivity(iq);
                    }
                    else{
                        Intent ir= new Intent(Calculator2.this,Calculator_3Activity.class);
                        startActivity(ir);
                    }

            }
        });

Deposit.java

btn2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
Intent iy = new Intent(Deposit.this,Calculator_3Activity.class);
startActivity(iy);

    }
});

Calculator3.java

 btn2.setOnClickListener(new OnClickListener() {
            // final boolean  val= getIntent().getExtras().getBoolean("tg");
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i1=new Intent(Calculator_3Activity.this ,Calculator_4Activity.class);
                startActivity(i1);
                //finish();
            }
        });

enter image description here

2 个答案:

答案 0 :(得分:2)

Calculator1.java

intent.putExtra("toggleBtn", tg.isChecked());
Calculator2.java

中的

final boolean val= getIntent().getExtras().getBoolean("toggleBtn");

而不是final boolean val= getIntent().getExtras().getBoolean("tg");

答案 1 :(得分:2)

<强> Calculator1.java

btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent intent = new Intent(Calculator_1Activity.this, Calculator2.class);
                intent.putExtra("toggleBtn", tg.isChecked());
                startActivity(intent);
        }
    });

<强> Calculator3.java

final boolean val= getIntent().getExtras().getBoolean(**"toggleBtn"**);