【问题标题】:Disabling RadioGroup to avoid multiple counts of a score禁用 RadioGroup 以避免多次计算分数
【发布时间】:2018-06-22 04:28:37
【问题描述】:

我正在尝试禁用我的 RadioGroup,以便用户无法重新选择,因此我的分数会增加。我在这里看到了一个实现:

How to disable a RadioGroup until checkbox is checked

尽管尝试这样做

我的 xml:

<RadioGroup
                android:id="@+id/rg1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/sm_margin"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/pates"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/pates"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/chips"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/chips"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/frites"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/frites"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/crepes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/def_margin"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/crepes"
                    android:onClick="onRadioButtonClicked" />

            </RadioGroup>
        </LinearLayout>

        <TextView
            android:id="@+id/question2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/def_margin"
            android:background="@drawable/layout_bg"
            android:fontFamily="@font/cambria"
            android:padding="@dimen/tv_padding"
            android:text="@string/question2" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="70dp"
            android:adjustViewBounds="true"
            android:src="@drawable/jc_fav" />

        <RadioGroup
            android:id="@+id/rg2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/sm_margin"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/mille_feuille"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/milleFeuille"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/creme_chantilly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cremeChantilly"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/opera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/opera"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/creme_brulee"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/def_margin"
                android:text="@string/cremeBrulee"
                android:onClick="onRadioButtonClicked" />

        </RadioGroup>

Java:

int score = 0;
RadioGroup rgQ1;
RadioGroup rgQ2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rgQ1 = findViewById(R.id.rg1);
    rgQ2 = findViewById(R.id.rg2);
 }


public void onRadioButtonClicked(View view) {

    // Is the button now checked? then assign into a boolean named 'checked'
    boolean checked = ((RadioButton) view).isChecked();

    // Question 1 logic
    // Check correct answer is checked and update score

    switch (view.getId()) {
        case R.id.frites:
            if (checked) score += 1;
            Log.v("MainActivity", "score" + score);
            break;
    }

    // Disable RadioButtons of rg1
    for (int i = 0; i < rgQ1.getChildCount(); i++) {
        (rgQ1.getChildAt(i)).setEnabled(false);
    }

    // Question 2 logic
    // Check correct answer is checked and update score

    switch (view.getId()) {
        case R.id.mille_feuille:
            if (checked) score += 1;
            Log.v("MainActivity", "score" + score);
            break;
    }

    // Disable RadioButtons of rg2
    for (int i = 0; i < rgQ2.getChildCount(); i++) {
        (rgQ2.getChildAt(i)).setEnabled(false);
    }
}

}

当一个项目被选中时,它会禁用第一个单选组,并且会禁用第二个(在用户为第二个单选组选择之前。

有什么想法吗?

【问题讨论】:

    标签: java android


    【解决方案1】:
    You are disabling your second radio group in the same click event. Check which radio button group is selected. 
    
    Like this, Similarly check group two and disable radio button group two!
    
    int selectedId = radioGroup.getCheckedRadioButtonId();
    if(selectedId != null){
    //Then disable radio button group one
    }
    

    【讨论】:

    • 运气不好.. 使用 switch (view.getId()) { case R.id.frites: if (checked) score += 1; Log.v("MainActivity", "score" + score);休息; } int selectedId = rgQ1.getCheckedRadioButtonId(); if(selectedId != null) { for (int i = 0; i
    • integer.HasValue,在 if 情况下使用它。比如 if(selectedId.HasValue){ }
    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2012-01-15
    相关资源
    最近更新 更多