【问题标题】:How to check the selected radiobutton?如何检查选定的单选按钮?
【发布时间】:2021-10-01 22:00:59
【问题描述】:

我尝试检查选中的单选按钮,

我找到了一个小细节,但是我无法粘贴。

现在我有一个(查看视图)错误。

程序不知道选择了哪一个,

我找不到解决办法。

我还能写什么?

请帮忙。

谢谢你:蚂蚁Z picture from the problem

我的代码:

package myapplication.Buttons;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
EditText editText;     
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textView = findViewById(R.id.text);
        Button button = findViewById(R.id.button);
        Button btn1 = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.exit);
       editText = findViewById(R.id.text);
        RadioGroup radioGroup =  findViewById(R.id.radioGroup1);

        editText.addTextChangedListener(new TextWatcher() {            
            //do something  }           
            });

        public void getSelectedRadioButton(View View ) {        <<< fail here :(View View )
            final RadioGroup radGroup = findViewById(R.id.radiogroup1);
            int radioID = radGroup.getCheckedRadioButtonId();
            RadioButton singleButton = findViewById(radioID);
        }
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup radioGroup = findViewById(R.id.radiogroup1); 

                                 <<< if i push the btn1 the program don't know the radioID>>>

                if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
                //do something
                }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
    } );
    }}

【问题讨论】:

  • View View 区分大小写。必须是:View view

标签: android view radio-button selecteditem android-radiogroup


【解决方案1】:

你应该做两件事来解决这个问题。

  1. 进行区分大小写的更正,并将View View更正为View view

  2. 将函数移到 onCreate 函数下方和 MainActivity 内。

  3. 你实际上并不需要 1&2 来获得你想要的。

  4. 您可以复制粘贴此代码并开始工作。

代码:

package myapplication.Buttons;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.uploadedlobster.PwdHash.R;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textView = findViewById(R.id.text);
        Button button = findViewById(R.id.button);
        Button btn1 = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.exit);
        editText = findViewById(R.id.text);
        RadioGroup radioGroup =  findViewById(R.id.radioGroup1);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
                    //do something
                }
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        } );
    }
}

编码愉快! :)

【讨论】:

  • 不幸的是,问题仍然存在。我贴了一张原始问题的图片,也许会有所帮助。
猜你喜欢
  • 2016-03-22
  • 1970-01-01
  • 2012-03-08
  • 2017-03-19
  • 2019-05-29
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多