【问题标题】:How to integrate radiobuttons in Tabhost?如何在 Tabhost 中集成单选按钮?
【发布时间】:2012-02-04 00:35:17
【问题描述】:

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

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


主要


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

}
}

【问题讨论】:

    标签: android sdk tabs radio-button


    【解决方案1】:

    在您想要单选按钮的活动中,.. 不是 TabActivty.. 请尝试以下类似的操作。注意:注意单选按钮的东西。我在发布之前撕掉了一些用于位置管理的代码。但是 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.. 只是一个开始,您需要将其包装在您的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多