如何通过使用现有活动中的另一个活动的切换按钮来转移控制流

时间:2013-01-31 08:58:29

标签: android android-intent toggle togglebutton

我已经创建了一个名为Activity1.xml,Activity2.xml,Activity3.xml,Activity4.xml的4个活动的应用程序。在activity1中我放了一个"切换按钮"用"是"和"不" ,现在我的第一个活动也有" next"按钮,当按下" activity2.xml"将被打开,它也有" next"按钮,现在重点是当" activity2"的下一个按钮时被按下它将检查" toggleButton" "活动1",如果是"是"它应该去" activity3"否则"活动4" ...我试过如下,但它不起作用请帮助我,实际上我不知道你是怎么做的所以请:

Activity1.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);
        }
    });
activity2.java

中的

  btn2.setOnClickListener(new OnClickListener() {

        @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);
                }

        }
    });

仍然无法正常工作

我已经放了照片以便更容易理解..

Activity1.xml enter image description here活性2 enter image description here Activity3 enter image description here Activity4 enter image description here

2 个答案:

答案 0 :(得分:0)

你将需要使用一个布尔字段来检查ToggleButton是否被点击或者没有点击其他活动,例如Activity1,Actitity2等...

使用Intent.putExtra将ToggleButton的状态发送到其他活动:

public static boolean status=false;
tg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (isChecked) {
             Intent ia = new Intent(Calculator_1Activity.this,Calculator2.class);
             status=true;
             ia.putExtra("tg_status", status); //<< send button status here
             startActivity(ia);
          } else {
              // The toggle is disabled
             Intent ib = new Intent(Calculator_1Activity.this,Calculator2.class);
             status=false;
             ia.putExtra("tg_status", status); //<< send button status here
             startActivity(ib);
           }
    }
});

在转到其他活动之前,单击是否使用tg_status检查按钮。

您可以在其他活动

中直接将其作为Calculator_1Activity.status进行访问

答案 1 :(得分:0)

当从活动1打开活动2时,在意图额外中将切换按钮的值作为布尔值(已检查或未检查)与其一起发送...并且仅在活动2中根据从以下值接收的值执行操作过程活动1 ...

   Intent intent = new Intent(this, NextActivity.class);
   intent.putExtras("toggleBtn", toggleButton.isChecked());

在activity2 / NextActivity中,通过

获取oncreate中的toggle btn的值
   boolean val= getIntent().getExtras().getBoolean("toggleBtn");

基于下一个btn点击监听器中的val start activity3或activity4