【问题标题】:Radio Button selection Changes Toast on Android单选按钮选择更改 Android 上的 Toast
【发布时间】:2020-01-11 02:48:12
【问题描述】:

我正在编写一个简单的测试应用程序。应用程序中有两个单选按钮。 id 是“radio_red”和“radio_blue”。我想创建一个 onClickListener 事件来读取与按钮关联的文本,然后返回基本的“正确”或“错误”toast。

以下是代码示例:

private OnClickListener radio_listener = new OnClickListener() {
    public void onClick(View v){
        RadioButton rb = (RadioButton) v;
        String ans =  rb.getText().toString();
        String an1 = "";

        if (ans.trim() == "Yes") {
            ans = "That's Right.";
        }
            else if (ans.trim() == "No") {
                ans = "thats wrong.";
            }
            else {
                ans = "none.";
            }

        Toast.makeText(v.getContext(), ans , Toast.LENGTH_SHORT).show();
    }

到目前为止还没有快乐。这是我的代码 sn-p。我检查了我的“main.xml”,并且正确引用了与按钮关联的文本。我添加了修剪以确保这一点。但是,吐司中返回的所有内容都是“无”。我错过了什么?提前感谢您的帮助。

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    您想处理RadioGroup,而不是RadioButtonRadioGroup 可以告诉您有关无线电控件的更多元详细信息,例如选中了哪个。例如,在你的监听器中你应该使用RadioGroup.getCheckedRadioButtonId()

    或者,您可以使用RadioGroup.setOnCheckedChangeListener() 向 RadioGroup 本身添加一个侦听器,该侦听器在后台设置一个切换。它可以告诉你哪个RadioButton被按下了。

    【讨论】:

    【解决方案2】:

    我之前也遇到过同样的问题....我已经找到了合适的解决方案。

    radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    Toast.makeText(Location.this, "" + radioButton.getText(), 2000).show(); 
                }
    });
    

    【讨论】:

      【解决方案3】:

      我认为你在这里写Condition时犯了一个错误,

      ans.trim() == "Yes"
      

      应该是,

      ans.trim().equalsIgnoreCase("YES")
      

      【讨论】:

        【解决方案4】:

        package com.example.rediocheckbox;
        
        import androidx.appcompat.app.AppCompatActivity;
        
        import android.annotation.SuppressLint;
        import android.location.Location;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;
        import android.widget.Toast;
        
        public class MainActivity extends AppCompatActivity {
        
            RadioGroup rad_grp;
            Button clear,submit;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                clear = findViewById(R.id.clear);
                submit = findViewById(R.id.submit);
                rad_grp = findViewById(R.id.rad_grp);
                rad_grp.clearCheck();
        
        
                rad_grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @SuppressLint("ResourceType")
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
        
                        RadioButton rb = group.findViewById(checkedId);
                        if (null != rb && checkedId > -1)
                        {
                        Toast.makeText(MainActivity.this,rb.getText(), Toast.LENGTH_SHORT).show();
                        }
                        }
                });
        
                clear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rad_grp.clearCheck();
                    }
                });
        
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RadioButton rb = rad_grp.findViewById(rad_grp.getCheckedRadioButtonId());
                        Toast.makeText(MainActivity.this,""+rb.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        
        
        }
        <?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"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">
        
            <RadioGroup
                android:id="@+id/rad_grp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        
                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male"/>
                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female"/>
                <RadioButton
                    android:id="@+id/other"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Other"/>
        
        
            </RadioGroup>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
        
                <Button
                    android:id="@+id/clear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Clear"
                    android:layout_marginLeft="20dp"/>
        
                <Button
                    android:id="@+id/submit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Submit"
                    android:layout_marginLeft="20dp"/>
        
            </LinearLayout>
        
        
        
        </LinearLayout>

        【讨论】:

        • 你可以通过editing 并解释它来改进这个答案。代码转储很少能提供好的答案,因为人们无法从它们那里学到很多东西,并且倾向于从网络上复制和粘贴一些代码。见How to Answer
        猜你喜欢
        • 2021-04-09
        • 1970-01-01
        • 2020-05-17
        • 1970-01-01
        • 2012-06-09
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多