【问题标题】:Android- RadioGroup setOnCheckedChangeListener() doesn't workAndroid- RadioGroup setOnCheckedChangeListener() 不起作用
【发布时间】:2016-05-10 17:10:38
【问题描述】:

我正在尝试为 RadioGroup 做一个监听器。我不知道为什么,但是当我检查任何一个单选按钮时,听众永远不会打电话。我想做一个简单的菜单来选择语言。 提前致谢!!

public class Language extends AppCompatActivity {
private static final String TAG = Menu.class.getSimpleName();

private RadioButton englishButton, portugueseButton, spanishButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.language);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(myToolbar);

    englishButton = (RadioButton) findViewById(R.id.english);
    englishButton.setChecked(true);
    portugueseButton = (RadioButton) findViewById(R.id.portuguese);
    portugueseButton.setChecked(false);
    spanishButton = (RadioButton) findViewById(R.id.spanish);
    spanishButton.setChecked(false);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);

    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d(TAG, "Get IN: setOnCheckedChangeListener");

            // find which radio button is selected
            if(checkedId == R.id.english) {
                portugueseButton.setChecked(false);
                spanishButton.setChecked(false);
                Toast.makeText(getApplicationContext(), "choice: English", Toast.LENGTH_SHORT).show();
            } else if(checkedId == R.id.portuguese) {
                englishButton.setChecked(false);
                spanishButton.setChecked(false);
                Toast.makeText(getApplicationContext(), "choice: Portuguese", Toast.LENGTH_SHORT).show();
            } else if(checkedId == R.id.spanish){
                englishButton.setChecked(false);
                portugueseButton.setChecked(false);
                Toast.makeText(getApplicationContext(), "choice: Spanish", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

这是我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="85dp"
            android:background="#670000"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView
                android:id="@+id/data_label"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="25dp"
                android:padding="10dip"
                android:text="@string/Language"
                android:textColor="#ffffff"
                android:textSize="25sp"
                android:textStyle="bold"/>


        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:orientation="horizontal"
            android:paddingLeft="30dp"
            android:paddingRight="30dp">
        <RadioButton android:id="@+id/english"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#670000"
            android:text="@string/English"
            android:textSize="40dp"/>
        </LinearLayout>

        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:orientation="horizontal"
            android:paddingLeft="30dp"
            android:paddingRight="30dp">
        <RadioButton android:id="@+id/portuguese"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#670000"
            android:text="@string/Portuguese"
            android:textSize="40dp"/>
        </LinearLayout>
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:orientation="horizontal"
            android:paddingLeft="30dp"
            android:paddingRight="30dp">
        <RadioButton android:id="@+id/spanish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#670000"
            android:text="@string/Spanish"
            android:textSize="40dp"/>
        </LinearLayout>

    </RadioGroup>

    </LinearLayout>

【问题讨论】:

    标签: android radio-group


    【解决方案1】:

    您无需声明 RadioButton(s) 并手动检查它们!这就是你应该如何使用 RadioGroup 和 RadioButton:

    在您的 XML 中:

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RadioButton
            android:id="@+id/A"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A"/>
    
        <RadioButton
            android:id="@+id/B"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B"/>
    </RadioGroup>
    

    在您的活动中:

    RadioGroup group = (RadioGroup)findViewById(R.id.group);
    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d(TAG, "changed");
            if(checkedId==R.id.A)
                Log.d(TAG, "A");
            else if(checkedId==R.id.B)
                Log.d(TAG, "B");
        }
    });
    

    如果您希望默认选中其中一个 RadioButton,只需添加 XML:

    android:checked="true"
    

    或在您的活动中:

    group.check(R.id.A);
    

    【讨论】:

    • 感谢奥马尔!我已经按照你说的做了,但我仍然无法进入监听器。那是我的问题!
    • 不客气!确实很奇怪......代码是你得到的一切吗?
    • 我已经在帖子中添加了我的 xml 文件。也许问题来自那里
    • 确实错误在您的 XML 中。您必须删除每个RadioButton 周围的LinearLayout。如果您想要填充或其他内容,只需将其添加到您的 &lt;RadioButton&gt; 标记中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-01-07
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多