当我按下片段中的单选按钮时,如何打印Toast文本?

时间:2018-05-21 04:44:24

标签: android radio-button radio-group

当我按下一个单选按钮时,我需要打印一个Toast Text,但是我需要在一个片段中执行此操作,按钮在一个放射线组中你可以帮助我吗? 当我启动应用程序,这没有打开片段只有它崩溃,并打印应用程序停止我认为问题是因为我创建了radiogroup它包含更多的项目,例如tableLayout。

this is the part of my XML

       <RadioGroupxmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:id="@+id/GroupCombo">
            <!--android:visibility="visible"-->
            <!--android:checkedButton="@+id/comprar">-->

            <TableLayout
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:layout_marginTop="20dp"
                android:background="#00cdff"
                android:orientation="horizontal"
                android:visibility="visible">


                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal">

                    <RadioButton
                        android:id="@+id/rcombo1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="COMBO 1"
                        android:textAlignment="textStart"
                        android:textSize="14sp"
                        android:onClick="onClick"/>

                </TableRow>

                <TableRow
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:gravity="center_horizontal"
                    android:orientation="vertical"
                    android:visibility="visible">

                    <TextView
                        android:id="@+id/textView13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="- Lavada general"
                        android:textColor="@android:color/black"
                        android:textSize="14sp" />
                </TableRow>

                <TableRow
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="- Aspirada"
                        android:textColor="@android:color/black"
                        android:textSize="14sp" />
                </TableRow>

                <TableRow
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="- Brillada"
                        android:textColor="@android:color/black"
                        android:textSize="14sp" />

                </TableRow>

                <TableRow
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/textView30"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="- Llantin"
                        android:textColor="@android:color/black"
                        android:textSize="14sp" />

                </TableRow>

                <TableRow
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_marginTop="10dp"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/textView14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="$10.000"
                        android:textAlignment="center"
                        android:textColor="@android:color/black"
                        android:textSize="14sp" />

                </TableRow>
            </TableLayout>

            <TableLayout
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:layout_marginTop="20dp"
                android:background="#00cdff"
                android:orientation="horizontal"
                android:visibility="visible">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal">

                    <RadioButton
                        android:id="@+id/rcombo2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/black_ops_one"
                        android:text="COMBO 2"
                        android:textAlignment="textStart"
                        android:textSize="14sp"
                        android:onClick="onClick" />
                      </RadioGroup>

this is my java code

public class fservicios extends Fragment {

Button button;
View view;
RadioButton radioButton;
//RadioGroup radioGroup;

Activity activity =getActivity();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

      view = inflater.inflate(R.layout.fragment_fservicios, container, false);
      view.findViewById(R.id.rcombo1).setOnClickListener((View.OnClickListener) this);
      view.findViewById(R.id.rcombo2).setOnClickListener((View.OnClickListener) this);
      view.findViewById(R.id.rcombo3).setOnClickListener((View.OnClickListener) this);
      view.findViewById(R.id.rcombo4).setOnClickListener((View.OnClickListener) this);

   return view;
}


public void onClick(View view) {

    boolean checked = ((RadioButton) view).isChecked();
    switch(view.getId()){

        case R.id.rcombo1:
            Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG ).show();

        case R.id.rcombo2:
            Toast.makeText(getActivity(), "B", Toast.LENGTH_LONG ).show();

        case R.id.rcombo3:
            Toast.makeText(getActivity(), "C", Toast.LENGTH_LONG ).show();

        case R.id.rcombo4:
            Toast.makeText(getActivity(), "D", Toast.LENGTH_LONG ).show();
    }
}

}

and this is the LogCat

05-21 00:09:42.448 30241-30241/jje.screen2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: jje.screen2, PID: 30241
    java.lang.ClassCastException: jje.screen2.fservicios cannot be cast to android.view.View$OnClickListener
        at jje.screen2.fservicios.onCreateView(fservicios.java:46)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6165)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

0 个答案:

没有答案