【问题标题】:How to use Radio button group id for switch, in Android, between different fragments如何在Android中使用单选按钮组ID在不同片段之间进行切换
【发布时间】:2016-06-29 19:15:03
【问题描述】:

我是新手。

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

文件说明:

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

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    这应该适合你

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

    希望这会有所帮助。

    【讨论】:

    • @Mr7,它不起作用,现在我也认为 id 是我自己在 switch 上方定义的变量,而 getId() 与获取 View 的 id 有关。
    • v.getId() 返回您当前单击的单选按钮 ID
    • 否则,您可以获取选中的单选按钮 id,如 int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
    • 它现在也不起作用,先生,我正在做的是从设置(片段)中获取选中的单选按钮 ID,并在开关中使用它,在主页(片段),两个片段出现在导航抽屉中。
    • 你能控制switch吗?
    【解决方案2】:

    你需要 setOnCheckedChangeListener。

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

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      相关资源
      最近更新 更多