2012-03-31 73 views
0

我有一個RadioGroup,裏面有兩個按鈕。根據所選的單選按鈕打開某個活動?

我有一個「創建」按鈕,當按下時,啓動兩個活動之一。如果第一個單選按鈕搜索在按下「創建」按鈕時被選中,它應該打開「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); 

     } 
     }); 
    }  
    } 
} 

回答

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); 
       } 
      } 
     } 
    }); 
+0

樣子回答我... :) – Mayank 2012-03-31 23:23:16

+0

謝謝,成功了! – Cole 2012-04-01 18:54:28