如何在Tabhost中集成单选按钮?

时间:2012-02-03 16:23:57

标签: android sdk tabs radio-button

我已经在Android SDK(2.3.3)中完成了三个TAB-GUI,但我想将几个radiobuttons和一个radiogroup集成到一个TAB中。我是一个android-newbe,所以我希望有人可以帮助我?

因此主代码和相应的类代码:


MAIN


package com.example.androidtablayout;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

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

    TabHost tabHost = getTabHost();

    // Tab for Dag
    TabSpec dagspec = tabHost.newTabSpec("Dag");
    dagspec.setIndicator("DagRooster",         getResources().getDrawable(R.drawable.icon_dag_tab));
    Intent dagIntent = new Intent(this, DagActivity.class);
    dagspec.setContent(dagIntent);

    // Tab for Norm
    TabSpec normspec = tabHost.newTabSpec("Norm");
    // setting Title and Icon for the Tab
    normspec.setIndicator("Normaal", getResources().getDrawable(R.drawable.icon_norm_tab));
    Intent normIntent = new Intent(this, NormActivity.class);
    normspec.setContent(normIntent);


    // Tab for Instel
    TabSpec instelspec = tabHost.newTabSpec("Instel");
    instelspec.setIndicator("Info", getResources().getDrawable(R.drawable.icon_setting_tab));
    Intent instelIntent = new Intent(this, InstelActivity.class);
    instelspec.setContent(instelIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(dagspec); // Adding photos tab
    tabHost.addTab(normspec); // Adding songs tab
    tabHost.addTab(instelspec); // Adding videos tab

        }
}

NORM-CLASS代码


package com.example.androidtablayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;



public class NormActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instel_layout);  

}
}

1 个答案:

答案 0 :(得分:0)

在活动中你想要radioButtons,.. Not TabActivty ..尝试下面这样的事情。注意:关注radiobutton的东西..在发布之前,我已经将一些代码用于位置管理。但是radioButton逻辑可以为你提供你想要的东西。

public class Distribute  extends Activity implements OnClickListener {

    @SuppressWarnings("unused")
    private static final String DEBUG_FLAG = "DEBUG_FLAG";
    private static final String SAVE_PREFS = "save_prefs";

    private static RadioButton mUserTypeRadioButton1;
    private static RadioButton mUserTypeRadioButton2;
    private static RadioButton mUserTypeRadioButton3;
    private static RadioButton mUserTypeRadioButton4;
    private static CheckBox    mSavePrefs;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        TextView textview = new TextView(this);
        textview.setText("Distribution");
        setContentView(R.drawable.configup);


        Button button = (Button)findViewById(R.id.btn_configup1);

        mUserTypeRadioButton1 = (RadioButton) findViewById(R.id.rb_configup1);
        mUserTypeRadioButton2 = (RadioButton) findViewById(R.id.rb_configup2);
        mUserTypeRadioButton3 = (RadioButton) findViewById(R.id.rb_configup3);
        mUserTypeRadioButton4 = (RadioButton) findViewById(R.id.rb_configup4);
        mSavePrefs = (CheckBox) findViewById(R.id.cb_config1);


        button.setOnClickListener(this);        
    }


    public void onClick(View v) {
        Intent i = new Intent();
        Bundle extras = new Bundle();

        int userType = 0;
        int savePrefs = 0;
        //i.putExtra("passLoc", currLoc);


        i.setClass(getApplicationContext(), Clevel.class);
        i.putExtras(extras);

        if (mUserTypeRadioButton1.isChecked()) {
             userType = 1;
        }
        else if (mUserTypeRadioButton2.isChecked()) {
             userType = 2;     
        }
        else if (mUserTypeRadioButton3.isChecked()) {
             userType = 3;
        }
        else if (mUserTypeRadioButton4.isChecked()) {
             userType = 4;
        } 
        if (mSavePrefs.isChecked()) {
             savePrefs = 1;
        }
        else {
             savePrefs = 0;
        }
        if (userType == 4){
            Toast.makeText(this, "Ad-Hoc Reporting is not Yet Implemented", 5).show();
            i.putExtra("SetTab", 1);
        }
        else if (userType == 0){
            Toast.makeText(this, "Please Select a Report Group", 5).show();
            i.putExtra("SetTab", 1);
        }
        else {
        i.putExtra("ds", userType);
        i.putExtra("prefs", savePrefs);
        i.putExtra("SetTab", 2);
        }       
        startActivity(i);

    }    

}

在你的TabHost中你需要这样的东西来管理Intent / Bundle

   /**
     * Distribute TabHost--------------------------------
     */
    intent = new Intent().setClass(this, Distribute.class);
    if(mTab == 1){
        mPos = extras.getInt("lvPos");
        mPrefs = extras.getInt("prefs");
        intent.putExtra("lvPos", mPos);
        intent.putExtra("prefs", mPrefs);
    }
    spec = tabHost.newTabSpec("Config").setIndicator("Distribute-It!",
                      res.getDrawable(R.drawable.gixconfig))
                  .setContent(intent);
    tabHost.addTab(spec);

所需的XML ..刚开始,你需要将这个包装在你希望radioButtons显示的屏幕的XML中

<RadioGroup
            android:id="@+id/rg_configup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            >
            <RadioButton
                android:id="@+id/rb_configup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sales"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Regional"
                android:layout_above="@+id/rb_configup1"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Global"
                android:layout_above="@+id/rb_configup2"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
            <RadioButton
                android:id="@+id/rb_configup4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ad-hoc"
                android:layout_above="@+id/rb_configup3"
                android:layout_alignLeft="@+id/rb_configup1"
                >
            </RadioButton>
        </RadioGroup>