首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

radiogroup中实现切换activity的有关问题

2012-04-16 
radiogroup中实现切换activity的问题想在radiogroup中通过点击radiobutton实现activity的切换,为什么显示

radiogroup中实现切换activity的问题
想在radiogroup中通过点击radiobutton实现activity的切换,为什么显示的总是前一个选择的activity
初始化显示第一个,第一次点击其他任意一个按钮显示的还第一个,如果第二次再点击另一个,那么就会显示第一次点击时应该显示的activity内容,后面情况类似,检查好几遍了,应该没有逻辑问题,部分代码如下:
public class TabbutActivity extends TabActivity implements
OnCheckedChangeListener {
private TabHost mHost;
private Intent buttonOneIntent;
private Intent buttonTwoIntent;
private Intent buttonThreeIntent;
private Intent buttonFourIntent;
private Intent buttonFiveIntent;

public static TabbutActivity instance = null;
public int[] TabIds = { R.id.radio_button0, R.id.radio_button1,
R.id.radio_button2, R.id.radio_button3, R.id.radio_button4, };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.buttonOneIntent = new Intent(this, ButtonOne.class);
this.buttonTwoIntent = new Intent(this, ButtonTwo.class);
this.buttonThreeIntent = new Intent(this, ButtonThree.class);
this.buttonFourIntent = new Intent(this, ButtonFour.class);
this.buttonFiveIntent = new Intent(this, ButtonFive.class);

instance = this;

((RadioButton) findViewById(R.id.radio_button0))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button1))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button2))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button3))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button4))
.setOnCheckedChangeListener(this);
setupIntent();
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
switch (buttonView.getId()) {
case R.id.radio_button0:
this.mHost.setCurrentTabByTag("buttonOne");
break;
case R.id.radio_button1:
this.mHost.setCurrentTabByTag("buttonTwo");
break;
case R.id.radio_button2:
this.mHost.setCurrentTabByTag("buttonThree");
break;
case R.id.radio_button3:
this.mHost.setCurrentTabByTag("buttonFour");
break;
case R.id.radio_button4:
this.mHost.setCurrentTabByTag("buttonFive");
break;
}
}
}
private void setupIntent() {

this.mHost = getTabHost();
TabHost mTabHost = this.mHost;
mTabHost.addTab(this.mHost
.newTabSpec("buttonOne")
.setIndicator(getString(R.string.one),
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(this.buttonOneIntent));
mTabHost.addTab(this.mHost
.newTabSpec("buttonTwo")
.setIndicator(getString(R.string.two),
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(this.buttonTwoIntent));
mTabHost.addTab(this.mHost
.newTabSpec("buttonThree")
.setIndicator(getString(R.string.three),
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(this.buttonThreeIntent));
mTabHost.addTab(this.mHost
.newTabSpec("buttonFour")
.setIndicator(getString(R.string.four),
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(this.buttonFourIntent));
mTabHost.addTab(this.mHost
.newTabSpec("buttonFive")
.setIndicator(getString(R.string.five),
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(this.buttonFiveIntent));
}
RadioGroup group = (RadioGroup) findViewById(R.id.main_radio);
group.check(TabIds[0]); // 初始状态显示第index项

}
其他的Activity中使用的是setContentView


public class ButtonTwo extends Activity{
private TextView buttonTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buttontwo);
buttonTwo = (TextView) findViewById(R.id.butttontwo);
buttonTwo.setText("BUTTON TWO!");
}
}
小弟刚学,求大神,先谢过了啊!!!!!

[解决办法]
你是抄的代码吧,有很多多余的代码,我改写了一下,能正常运行

Java code
import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.TabHost;import android.widget.CompoundButton.OnCheckedChangeListener;public class TabbutActivity extends TabActivity implements OnCheckedChangeListener {    private TabHost mHost;    /** Called when the activity is first created. */    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mHost = getTabHost();        mHost.addTab(mHost.newTabSpec("buttonOne").setIndicator(                getString(R.string.one),                getResources().getDrawable(R.drawable.ic_launcher))                .setContent(new Intent(this, ButtonOne.class)));        mHost.addTab(mHost.newTabSpec("buttonTwo").setIndicator(                getString(R.string.two),                getResources().getDrawable(R.drawable.ic_launcher))                .setContent(new Intent(this, ButtonTwo.class)));        mHost.addTab(mHost.newTabSpec("buttonThree").setIndicator(                getString(R.string.three),                getResources().getDrawable(R.drawable.ic_launcher))                .setContent(new Intent(this, ButtonThree.class)));        mHost.setCurrentTab(0);           ((RadioButton) findViewById(R.id.radio_button0))                .setOnCheckedChangeListener(this);        ((RadioButton) findViewById(R.id.radio_button1))                .setOnCheckedChangeListener(this);        ((RadioButton) findViewById(R.id.radio_button2))                .setOnCheckedChangeListener(this);        //setupIntent();    }    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        // TODO Auto-generated method stub        if (isChecked) {            switch (buttonView.getId()) {            case R.id.radio_button0:                this.mHost.setCurrentTabByTag("buttonOne");                break;            case R.id.radio_button1:                this.mHost.setCurrentTabByTag("buttonTwo");                break;            case R.id.radio_button2:                this.mHost.setCurrentTabByTag("buttonThree");                break;            }        }    }    } 

热点排行