android中的简单单选按钮操作

时间:2013-11-15 05:00:36

标签: android

我正在尝试制作一组​​RadioButtons <{1}} <{1}} onclick button
  • 有两组单选按钮
  • 直到我点击选择日期,必须显示按钮组 隐形
  • 当我点击selectdays然后它们必须是可见的

enter image description here

我尝试了什么::

  • 我已经设置了整个设置
  • 但不知道如何实现我上面提到的行动

buffet_offerings_breakfast_menu2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="10dp" 
    android:id="@+id/ChangeBreakfastFragment">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="140dp"
        android:background="@drawable/frame_background_for_rounded_corners_white_bkg" >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_view_background_white_background"
            android:padding="10dp" />

        <LinearLayout
            android:id="@+id/linearLayout_lunch_first_row_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/lunch_button_id"
                    android:layout_width="97dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/green_button_rounded_corners_buffetofferings"
                    android:text="Breakfast" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/lunch_from_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/image1"
                    android:layout_toRightOf="@+id/lunch_button_id"
                    android:gravity="center"
                    android:paddingLeft="30dp"
                    android:text="From"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/from_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/lunch_to_textview_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/lunch_button_id"
                    android:layout_toRightOf="@+id/textView1"
                    android:gravity="center"
                    android:text="To"
                    android:textColor="#000000"
                    android:textSize="15sp" />

                <requestFocus />

                <EditText
                    android:id="@+id/to_lunch_edit_text_id"
                    android:layout_width="55dp"
                    android:layout_height="wrap_content"
                    android:ems="10" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout_type_of_days"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_below="@+id/linearLayout_lunch_first_row_id"
            android:gravity="center|top"
            android:orientation="horizontal"
            android:paddingLeft="5dp"
            android:paddingTop="10dp" >

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/BreakfastRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:checked="true"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Weekdays"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/LunchRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Weekends"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Select Days"
                    android:textSize="15dp" />
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout_individualdays"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_below="@+id/linearLayout_type_of_days"
            android:gravity="center|top"
            android:orientation="horizontal"
            android:paddingLeft="5dp"
            android:paddingTop="10dp" >

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/BreakfastRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:checked="true"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Mon"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/LunchRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Tue"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Wed"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/BreakfastRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:checked="true"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Thu"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/LunchRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Fri"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Sat"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Sun"
                    android:textSize="15dp" />
            </RadioGroup>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

BuffetOfferings_MainFragmentActivity.java

public class BuffetOfferings_MainFragmentActivity extends FragmentActivity{

    FragmentManager manager;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buffet_offerings_fragment_main_reference);

        Buffet_offerings_breakfast_menu1 breakfast_fragment=new Buffet_offerings_breakfast_menu1();
        Buffet_offerings_lunch_menu1 lunch_fragment=new Buffet_offerings_lunch_menu1();
        Buffet_offerings_dinner_menu1 dinner_fragment=new Buffet_offerings_dinner_menu1();

        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();

        transaction.add(R.id.BREAKFAST_LAYOUT_ID,breakfast_fragment, "breakfast_menu1_fragment");
        transaction.add(R.id.LUNCH_LAYOUT_ID,lunch_fragment, "lunch_menu1_fragment");
        transaction.add(R.id.DINNER_LAYOUT_ID,dinner_fragment, "dinner_menu1_fragment");

        transaction.commit();
    }

    public void BreakfastRedMenu1(View V){
        Buffet_offerings_breakfast_menu2  breakfast_fragment1=new Buffet_offerings_breakfast_menu2();
        transaction=manager.beginTransaction();
        transaction.replace(R.id.BREAKFAST_LAYOUT_ID, breakfast_fragment1, "Tag1");
        transaction.commit();
    }

    public void LunchRedMenu1(View V){
        Buffet_offerings_lunch_menu2  lunch_fragment1=new Buffet_offerings_lunch_menu2();
        transaction=manager.beginTransaction();
        transaction.replace(R.id.LUNCH_LAYOUT_ID, lunch_fragment1, "Tag2");
        transaction.commit();
    }

    public void DinnerRedMenu1(View V){
        Buffet_offerings_dinner_menu2  dinner_fragment1=new Buffet_offerings_dinner_menu2();
        transaction=manager.beginTransaction();
        transaction.replace(R.id.DINNER_LAYOUT_ID, dinner_fragment1, "Tag3");
        transaction.commit();
    }
}

Buffet_offerings_breakfast_menu2.java

public class Buffet_offerings_breakfast_menu2 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.buffet_offerings_breakfast_menu2, container, false);
        return view;
    }
}

编辑后

Buffet_offerings_breakfast_menu2.java

public class Buffet_offerings_breakfast_menu2 extends Fragment{

    RadioGroup radioGroup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.buffet_offerings_breakfast_menu2, container, false);


        radioGroup = (RadioGroup) getActivity().findViewById(R.id.radioGroup1);        

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
          public void onCheckedChanged(RadioGroup group, int checkedId) {
             // checkedId is the RadioButton selected
                   if(checkedId == R.id.DinnerRG_ID)
                   {
                      getActivity().findViewById(R.id.radioGroup2).setVisibility(View.VISIBLE);
                   }
          }
        });


        return view;
    }



}

记录 ::

11-15 11:43:02.391: E/AndroidRuntime(311): FATAL EXCEPTION: main
11-15 11:43:02.391: E/AndroidRuntime(311): java.lang.NullPointerException
11-15 11:43:02.391: E/AndroidRuntime(311):  at com.project.findmybuffet.Buffet_offerings_breakfast_menu2.onCreateView(Buffet_offerings_breakfast_menu2.java:25)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.os.Handler.handleCallback(Handler.java:587)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
11-15 11:43:02.391: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 11:43:02.391: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:43:02.391: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
11-15 11:43:02.391: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 11:43:02.391: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 11:43:02.391: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

获取ID,

view.findViewById(...);

原始答案:

在XML布局中:

  • 将第二个广播组的名称更改为 radioGroup2
  • 现在让我们通过添加以下内容使第二个radioGroup不可见: android:visibility =“gone”

现在,如果您运行该应用程序,您将只获得第一行内容。现在让我们添加代码,以便在收听selectDays点击时显示单选按钮:

  • 在片段onCreateView()中:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
      public void onCheckedChanged(RadioGroup group, int checkedId) {
         // checkedId is the RadioButton selected
               if(checkedId == R.id.**#**){
                  findViewById(R.id.radioGroup2).setVisibility(View.visible);
               }
      }
    });
    

注意:

  • 必须为所有xml视图提供唯一ID,以便我们可以在运行时访问它们。
  • 是selectDays radioButton的ID。