java - 如何在 Android 中使用单选按钮组 ID 在不同 fragment 之间进行切换

标签 java android android-fragments

我是新手。

在应用中使用导航栏、 fragment 。

文件说明:

fragment_settings.xml:包含单选按钮组

SettingsFragment.java: 主要用于在抽屉导航中单击“设置”时显示 fragment_settings.xml。我检查了不同的

MainFragment.java:这里,我想使用RadioButton id,它被点击进入switch case,然后执行相应的操作。

但问题是控制权在开关中而不是在案例中,我尝试通过放置 toast 但 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;
                    }
                }
}
        });

最佳答案

这应该适合你

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

希望这对您有所帮助。

关于java - 如何在 Android 中使用单选按钮组 ID 在不同 fragment 之间进行切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094400/

相关文章:

java - 使用 Lightweight-Stream-API 重写 java 8 方法以用于 api 21

javamail AuthenticationFailedException : EOF on socket

Android - 调整照片大小和添加水印会导致图像质量差

android - E/广告 : Fail to get isAdIdFakeForDebugLogging with error code 3

android - 如何以编程方式在android中使用paypal将钱从一个帐户转移到另一个帐户

android - fragment 中的 findViewById

android - 避免在 ListView 中重新添加项目

java - 使用 printf 给出两位小数并进行输入

java - Android - 在提交之前(期间)检查 FragmentManager 事务是否存在由 fragment 代码引起的异常

java - Antlr 左分解语法