如何设置从其他活动检查的单选按钮?

时间:2015-12-17 11:36:13

标签: java android android-activity

我有两项活动。第一个活动有不同的布局,可以打开另一个活动。在另一个活动中,我有一个分段的单选按钮组,我想在第一个活动中单击基于布局检查它。要检查我使用过的单选按钮。

segmented5.check(R.id.pickupbakButton);

这会设置按钮,但我希​​望根据我在布局中进行其他更改来检查其他活动。

我有第一个这样的活动:enter image description here

然后说点击PICKUPBOX / BLIND VAN

enter image description here

应该显示拾取器/盲车的单选按钮。

我怎样才能实现这个目标?

GoBoxActivity

public class GoBoxActivity extends AppCompatActivity {

private Intent i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_go_box);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("GO-BOX");
    setSupportActionBar(toolbar);

    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

   final GoBoxPickupActivity gobox = new GoBoxPickupActivity();

   LinearLayout pickupBak = (LinearLayout)findViewById(R.id.PickupBak);
    LinearLayout picupBox = (LinearLayout)findViewById(R.id.pickupbox);
    LinearLayout engkelBak = (LinearLayout)findViewById(R.id.engkelBak);
    LinearLayout engkelBox = (LinearLayout)findViewById(R.id.engkelBox);

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

            i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
            startActivity(i);
        }
    });

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

            i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
            startActivity(i);
        }
    });

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

            i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
            startActivity(i);
        }
    });
    engkelBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
            startActivity(i);
        }
    });
  }
}

PickupBoxActivity

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_go_box_pickup);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
segmented5 = (SegmentedGroup) findViewById(R.id.segmented3);

    segmented5.setOnCheckedChangeListener(this);

 @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.pickupbakButton:

                engkelTextview.setVisibility(View.GONE);
                extraEngkelTextView.setVisibility(View.GONE);
                airConditioning.setVisibility(View.GONE);
                picBoxTextview.setVisibility(View.VISIBLE);
                extraPickBoxTextview.setVisibility(View.VISIBLE);

                return;

            case R.id.pickupboxButton:

                engkelTextview.setVisibility(View.GONE);
                extraEngkelTextView.setVisibility(View.GONE);
                airConditioning.setVisibility(View.VISIBLE);
                picBoxTextview.setVisibility(View.VISIBLE);
                extraPickBoxTextview.setVisibility(View.VISIBLE);

                return;

            case R.id.engkelbakButton:

                picBoxTextview.setVisibility(View.GONE);
                extraPickBoxTextview.setVisibility(View.GONE);
                engkelTextview.setVisibility(View.VISIBLE);
                extraEngkelTextView.setVisibility(View.VISIBLE);
                airConditioning.setVisibility(View.INVISIBLE);

                return;

            case R.id.engkelboxButton:

                picBoxTextview.setVisibility(View.GONE);
                extraPickBoxTextview.setVisibility(View.GONE);
                engkelTextview.setVisibility(View.VISIBLE);
                extraEngkelTextView.setVisibility(View.VISIBLE);
                airConditioning.setVisibility(View.VISIBLE);

                return;
            default:

                engkelTextview.setVisibility(View.GONE);
                extraEngkelTextView.setVisibility(View.GONE);
                airConditioning.setVisibility(View.GONE);
                picBoxTextview.setVisibility(View.VISIBLE);
                extraPickBoxTextview.setVisibility(View.VISIBLE);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在onClick GoBoxActivity 中修改你的代码,如下所示

i=new Intent(getApplicationContext(),GoBoxPickupActivity.class);
i.putExtra("selected", 1);
startActivity(i);

PickupBoxActivity 中,执行以下操作

int selected = getIntent().getIntExtra("selected",1);

之后

switch(selected)
{
    case 1:
      segmented5.check(R.id.pickupbakButton);
    break;
    case 2:
       .... So on
}

让我进一步了解任何问题..