繁体   English   中英

单击按钮时获取所选单选按钮的ID

[英]Getting the id of the selected radio button when a button is clicked

这是我要执行的操作:我有2个活动,一个MainActivity和一个SettingsActivity,单击MA中的一个按钮即可打开。 在SA中,有6个单选按钮,每个单选按钮在单选组中具有对应的颜色ID(如if / else语句所示),以及一个用于关闭活动的按钮。 当用户按下关闭按钮时,MA的背景将更改为用户选择的颜色。 一切正常,除了getCheckedRadioButtonId()方法获取创建SettingsActivity时选择的按钮的ID(无),因此显示的颜色始终是最后else块中的颜色。 我知道这应该是一个非常简单的修复程序,但是我花了大约4个小时尝试了所有我能想到的解决方案,而且我的许多google搜索都没有给我任何有用的信息。以下是相关代码:

SettingsActivity.java:

public class SettingsActivity extends AppCompatActivity {                                  

    public static final String colorID = "colorID";                                        

    @Override                                                                              
    protected void onCreate(Bundle savedInstanceState) {                                   
        super.onCreate(savedInstanceState);                                                
        setContentView(R.layout.activity_settings);                                        
        LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);              
        View settingsView= inflater.inflate(R.layout.activity_settings, null);             

    returnToMain();                                                                                                                                                                                                                                    
    }                                                                                      

    public int getColorValueID(View view) {                                                

        final int colorId;                                                                 
          int id;                                                                          

        final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);     

                if(radioGroup.getCheckedRadioButtonId()!= -1) {                            
                    id = radioGroup.getCheckedRadioButtonId();                             
                }                                                                          
                else {                                                                     
                    id = 0;                                                                
                }                                                                          

        if (id == R.id.redButton) {                                                        

            colorId = R.color.red_theme;                                                   

        } else if (id == R.id.blueButton) {                                                

            colorId = R.color.blue_theme;                                                  

        } else if (id == R.id.greenButton) {                                               

            colorId = R.color.green_theme;                                                 

        } else if (id == R.id.orangeButton) {                                              

            colorId = R.color.orange_theme;                                                

        } else if (id == R.id.purpleButton) {                                              

            colorId = R.color.purple_theme;                                                

        } else {                                                                           

            colorId = R.color.blue_theme;                                                  

        }                                                                                  

        return colorId;                                                                    
    }                                                                                      

    public void returnToMain(){                                                            
        Button returnButton = (Button) findViewById(R.id.backToMain);                      

        returnButton.setOnClickListener(new View.OnClickListener() {                       
            @Override                                                                      
            public void onClick(View view) {                                               
                LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);      
                View settingsView= inflater.inflate(R.layout.activity_settings, null);     

                int color = getColorValueID(settingsView);                                 
                final Intent mIntent = new Intent();                                       
                mIntent.putExtra(colorID, color);                                          
                setResult(RESULT_OK, mIntent);                                             
                finish();                                                                  
            }                                                                              
        });                                                                                
    }                                                                                                                                                                              
}                              

activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rileybolen.simplemortgagecalculator.SettingsActivity"
    tools:background="#dedede">

    <TextView
        android:text="@string/settings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/settingsHeading"
        android:textColor="#494949"
        android:textSize="32sp"
        android:textAlignment="center"
        />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/themeHeading"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:id="@+id/radioGroup">

                <RadioButton
                    android:text="Orange"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/orangeButton"
                    android:layout_alignParentTop="true"
                    android:textColor="@color/orange_theme"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"
                    />

                <RadioButton
                    android:text="Purple"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/purpleButton"
                    android:layout_below="@id/orangeButton"
                    android:textColor="@color/purple_theme"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"/>

                <RadioButton
                    android:text="Grey"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/greyButton"
                    android:layout_below="@id/purpleButton"
                    android:textColor="#494949"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"/>


                <RadioButton
                    android:text="Red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/redButton"
                    android:layout_alignParentTop="true"
                    android:textColor="@color/red_theme"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"/>

                <RadioButton
                    android:text="Blue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/blueButton"
                    android:layout_below="@id/redButton"
                    android:textColor="@color/blue_theme"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"/>

                <RadioButton
                    android:text="Green"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/greenButton"
                    android:layout_below="@id/blueButton"
                    android:textColor="@color/green_theme"
                    android:textSize="18sp"
                    android:layout_marginLeft="20dp"/>


    </RadioGroup>

    <TextView
        android:text="@string/theme_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:id="@+id/themeHeading"
        android:textColor="#494949"
        android:textSize="24sp"
        android:layout_below="@+id/settingsHeading"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>



        <Button
            android:text="Close"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/backToMain"
            android:layout_alignParentBottom="true"
            />


</RelativeLayout>

在您的RadioGroup上注册一个onCheckedChanged(RadioGroup group, int checkedId)侦听器

radioGroup.setOnCheckedChangedListener(new OnCheckChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {

}

});

像这样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM