我有两个单选按钮,它调用相同的微调器数组。所以基于单选按钮如何调用两个活动。我的代码如下: 在这个我使用两个单选按钮,微调器和xml按钮
A.java
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)。谢谢。
答案 0 :(得分: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
}
}
答案 1 :(得分: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);
}
}
}
});
答案 2 :(得分: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
}
}