根据选择的单选按钮打开某个活动?

时间:2012-03-31 22:52:08

标签: android android-intent radio-button radio-group

我有一个带有两个按钮的RadioGroup。

我有一个“创建”按钮,按下后,启动两个活动中的一个。如果在按下“创建”按钮时选中第一个RadioButton 搜索,则应打开“ProductSearch”活动。

如果在按下“创建”按钮时选中了第二个RadioButton,类型,则应打开“TypeEntries”活动。但是现在,无论按哪个RadioButton,它都会打开“ProductSearch”。

因此适用于搜索,但不适用于类型

  RadioButton search = (RadioButton) findViewById(R.id.radio1);
  RadioButton type = (RadioButton) findViewById(R.id.radio2);

  Button create = (Button) findViewById(R.id.create);
  if(search.isChecked()){
      create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent createIntent = new Intent(getApplicationContext(), ProductSearch.class); // <----- START "SEARCH" ACTIVITY
            startActivityForResult(createIntent, 0);
        }
    });
  }else if(type.isChecked()){
      create.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent typeIntent = new Intent(getApplicationContext(), TypeEntries.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
            startActivityForResult(typeIntent, 0);

        }
      });
    }    
  } 
}

1 个答案:

答案 0 :(得分:2)

试试这个:

RadioButton search = (RadioButton) findViewById(R.id.radio1);
  RadioButton type = (RadioButton) findViewById(R.id.radio2);

  Button create = (Button) findViewById(R.id.create);
  create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        if(search.isChecked()){
            Intent createIntent = new Intent(getApplicationContext(), ProductSearch.class); // <----- START "SEARCH" ACTIVITY
            startActivityForResult(createIntent, 0);
            }
            else
            {
              if(type.isChecked())
              {
              Intent typeIntent = new Intent(getApplicationContext(), TypeEntries.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
              startActivityForResult(typeIntent, 0);
              }
            }
        }
    });
相关问题