如何在Android中使用单选按钮组ID在不同的片段

时间:2016-06-29 08:38:47

标签: java android android-fragments

我是新手。

在应用中使用导航栏,片段。

文件说明:

fragment_settings.xml:包含单选按钮组

SettingsFragment.java:基本上显示fragment_settings.xml,在导航栏中单击“设置”时。我检查了不同的

MainFragment.java:在这里,我想使用RadioButton id,它被点击到开关案例,然后相应地执行操作。

但问题是控制是在交换机中进行但不是在这种情况下,我尝试通过烤面包但但id不匹配任何情况。

fragment_settings.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.threefriends.filecrypto.SettingsFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Choose algorithm:"
            android:textSize="18dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="71dp"
        />
        <RadioGroup
            android:id="@+id/radioAlgorithms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/radioaes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AES (Default)"
                android:checked="true"
                android:layout_marginLeft="5dp"
            />
            <RadioButton
                android:id="@+id/radiodes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DES"
                android:layout_marginLeft="5dp"
                />
            <RadioButton
                android:id="@+id/radioidea"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IDEA"
                android:layout_marginLeft="5dp"
            />
            <RadioButton
                android:id="@+id/radiocustom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Custom"
                android:layout_marginLeft="5dp"
                />
        </RadioGroup>
    </LinearLayout>

</FrameLayout>

SettingsFragment.java

    package com.threefriends.filecrypto;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class SettingsFragment extends Fragment
{


    public SettingsFragment() {
        // Required empty public constructor
    }


    RadioGroup radiogroup;
    RadioButton aes, des, idea, custom;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View view=inflater.inflate(R.layout.fragment_settings, container, false);
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_settings, container, false);
        return view;
    }
}

MainFragment.java

 public class MainFragment extends Fragment {
    SettingsFragment settingsfragment;

    //Defined for file edittext.
    EditText editText2;

    public MainFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
       });
        final int id = ((RadioGroup)view.findViewById(R.id.radioAlgorithms)).getCheckedRadioButtonId();
        final Button b2 = (Button) view.findViewById(R.id.encb);    //Encrypt Button
        Button b3 = (Button) view.findViewById(R.id.decb);      //Decrypt Button

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplicationContext(), "Starting..."+R.id.radioAlgorithms, Toast.LENGTH_SHORT).show();
                // click related code goes here
                switch(id)    //This id is not matching to any case.
                {
                    case R.id.radioaes: {
                               //Code for aes algorithm.
                               break;
                    case R.id.radiodes: {
                               //Code for des algorithm.
                               break;
                     }
                    case R.id.radioidea: {
                               //Code for idea algorithm.
                               break;
                    }
                    case R.id.radiocustom: {
                                //My custom algorithm code.
                                break;
                    }
                }
}
        });

2 个答案:

答案 0 :(得分:3)

这应该对你有用

final RadioGroup group= (RadioGroup) findViewById(R.id.radioAlgorithms);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        int id = group.getCheckedRadioButtonId();
        switch (id) {
            case R.id.radioaes:
                // Your code
                break;
            case R.id.radiodes:
                // Your code
                break;
            case R.id.radioidea:
                // Your code
                break;
            case R.id.radiocustom:
                // Your code
                break;
            default:
                // Your code
                break;
        }
    }
});

希望这会有所帮助。

答案 1 :(得分:0)

你需要setOnCheckedChangeListener。

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }
});