0

我有两个单选按钮,它调用相同的微调器数组。所以基于单选按钮如何调用两个活动。我的代码如下:在这个我使用两个单选按钮,微调器和 xml 中的一个按钮

爪哇

     Spinner selectcity;
 RadioGroup rg = null;
 RadioButton rb1,rb2;
 Button searchbutton;
     rg = (RadioGroup) findViewById(R.id.radioGroup);
 rb1=(RadioButton)findViewById(R.id.seekers);
 rb2=(RadioButton)findViewById(R.id.posting);  
 selectcity = (Spinner) findViewById(R.id.statespinners);
     rb1.setOnClickListener(new View.OnClickListener()  
     {
     @Override
     public void onClick(View v) {
// TODO Auto-generated method stub
selectcity=(Spinner)findViewById(R.id.statespinners);
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
SearchCity.this, R.array.cities, android.R.layout.simple_spinner_item);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
  selectcity.setAdapter(adapter);   
  }
  });
  rb2.setOnClickListener(new View.OnClickListener()  
  {
  @Override
 public void onClick(View v) {
// TODO Auto-generated method stub
selectcity=(Spinner)findViewById(R.id.statespinners);
ArrayAdapter<CharSequence> adapter1 =  ArrayAdapter.createFromResource(
SearchCity.this, R.array.area, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
selectcity.setAdapter(adapter1);
}        
});
searchbutton=(Button)findViewById(R.id.statebutton);
searchbutton.setOnClickListener(new View.OnClickListener() {        
@Override
 public void onClick(View v) {                    


  //here i am strucked how to call two activities based on radio buttons selection
    }
    });

如何使用两个单选按钮更改并调用两个不同的活动。(A和B)。提前致谢。

4

3 回答 3

1

我认为你正在为此苦苦挣扎。您可以参考单选组并用于getCheckedRadioButtonId ()获取选中的单选按钮 ID。

int id = radioGroup.getCheckedRadioButtonId();
if (id == -1){
//no item selected
}
else{
if (id == R.id.seekers){
    //call activity A
}
if (id == R.id.posting){
    //call activity B
}
}
于 2013-10-24T07:24:39.067 回答
0
   Do like this.

searchbutton=(Button)findViewById(R.id.statebutton);
searchbutton.setOnClickListener(new View.OnClickListener() {
  @Override
public void onClick(View v) {
if(rb1.isChecked()){ //radio button rb1
// TODO Auto-generated method stub
Intent intent=new Intent(this,ActivityA.class);
startActivity(intent);
}
else
{
if(rb2.isChecked())//radio button rb2
{
Intent intent=new Intent(this,ActivityB.class);
startActivity(intent);
}
}
}

   });
于 2013-10-24T07:28:59.523 回答
0
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){            
if(checkedId == R.id.seekers)
{
// Call First Activity
}else if(checkedId == R.id.posting)
{
// Call Second Activity
}
}
于 2013-10-24T07:31:28.593 回答