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

发布于 2025-01-01 17:38:40 字数 2475 浏览 1 评论 0原文

我已经在 Android SDK (2.3.3) 中制作了一个完整工作的三个 TAB-GUI,但我想将几个单选按钮和一个单选组集成到一个 TAB 中。我是 android 新手,所以我希望有人可以帮助我?

主代码和相应的类代码如下:


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);  

}
}

I've made a full working three TAB-GUI in Android SDK (2.3.3) but I want to integrate several radiobuttons and one radiogroup into a TAB. I'm a android-newbe so I hope somebody can help me?

Hereby the main-code and the according Class-code:


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 code


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);  

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-08 17:38:40

在您想要 radioButtons 的活动中,.. 不是 TabActivty.. 尝试如下所示的操作。注意:请注意单选按钮的内容。我在发布之前撕下了一些位置管理代码。但单选按钮逻辑会让你得到你想要的。

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。首先,你需要将其包装在 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>

In the activity you want the radioButtons, .. Not TabActivty.. try something like this below. NB: pay atention to the radiobutton stuff.. I ripped some code for location management out before posting. But the radioButton logic will get you what you want.

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);

    }    

}

In your TabHost you will need something like this to manage the 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);

And the needed XML.. Just a start, you'll need to wrap this inside your XML for the screen you want radioButtons to show up

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文