2012-03-17 3 views
0

tラジオボタンが2つあります。アプリケーションがロードされると、1がデフォルトとしてtrueに設定されます。 1を選択すると表2が非表示になり、ラジオボタン2を選択すると表1が非表示になります。 これは、テーブルは開いていて、ラジオボタン2を選択するまでは動作します。だから私は私の最初の負荷で何かを見逃しています。開始時にラジオボタンの選択をロードする

正しい方向に軽く触れていただければ幸いです。

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RadioGroup; 
import android.widget.TableLayout; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class Slab extends Activity { 
private RadioGroup RadioMeat; 

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

    RadioMeat = (RadioGroup) findViewById(R.id.rgMeat);   
    RadioMeat.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 

     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch (checkedId) { 
      case R.id.rbBeef : 
       if (checkedId == R.id.rbBeef) { 
        TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1); 
        tl.setVisibility(View.VISIBLE); 
        TableLayout tl1 = (TableLayout)findViewById(R.id.tableLayout2); 
        tl1.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.rbPork : 
       if (checkedId == R.id.rbPork) { 
        TableLayout tl = (TableLayout)findViewById(R.id.tableLayout2); 
        tl.setVisibility(View.VISIBLE); 
        TableLayout tl1 = (TableLayout)findViewById(R.id.tableLayout1); 
        tl1.setVisibility(View.GONE); 
       } 
       break; 

     } 
     } 
    }); 
} 

}

XML

<RadioGroup 
     android:id="@+id/rgMeat" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RadioButton 
      android:id="@+id/rbBeef" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Beef" 
      android:checked="true"/> 

     <RadioButton 
      android:id="@+id/rbPork" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Pork" /> 
    </RadioGroup> 

答えて

1

あなたが直面している問題は、あなたのonCreate()が最初に呼び出され、開始の時点、で、onCheckedChanged()が呼び出されることはありませんということです。あなたがそれをチェックしたときにだけ呼び出されます。それはxmlを介して行うのではなく、プログラムでチェックしても呼ばれていました。 。だから、あなたはそれをあなたの上にある2つの方法、

()

コール

((TableLayout)findViewById(R.id.tableLayout2))setVisibility(View.GONE)を作成を行うことができ、

ラジオボタンの

またはsetChecked(

を呼び出します)1.

+0

後者の溶液を – bluejamesbond

+0

トリック、感謝メイトをしたトリックを行う必要があります。 – Calvin

関連する問題